虛擬機環境
ubuntu12.04
開發板
EasyARM-i.MX280A: 64m sdram 128M nandflash 運行官方提供的Linux-2.6.35.3內核linux
首先,如果需要使用curl解析https需要先編譯openssl庫,編譯這個庫可以參考
http://blog.csdn.net/andylauren/article/details/53456340
現在我們先講PC端編譯庫
1、下載源碼
2、解壓包並進入目錄
linux@ubuntu:~/opt/curl/curl-7.57.0$ tarxvf curl-7.57.0.tar.gz
linux@ubuntu:~/opt/curl/curl-7.57.0$ cdcurl-7.57.0/
3、輸入如下配置
3.1只編譯靜態庫
CPPFLAGS="-I/home/linux/opt/openssl/-I/home/linux/opt/openssl/include"LDFLAGS="-L/home/linux/opt/openssl/lib" LIBS="-ldl"./configure --with-ssl --disable-shared --enable-static --disable-dict--disable-ftp --disable-imap --disable-ldap --disable-ldaps --disable-pop3--disable-proxy --disable-rtsp --disable-smtp --disable-telnet --disable-tftp--disable-zlib --without-ca-bundle --without-gnutls --without-libidn--without-librtmp --without-libssh2 --without-nss --without-zlib--prefix=/home/linux/opt/curl
正常是不會遇到問題,遇到問題再具體解決然後執行make&make install
configure: Configured to build curl/libcurl: curl version: 7.57.0 Host setup: i686-pc-linux-gnu Install prefix: /home/linux/opt/curl Compiler: gcc SSL support: enabled (OpenSSL) SSH support: no (--with-libssh2) zlib support: no (--with-zlib) brotli support: no (--with-brotli) GSS-API support: no (--with-gssapi) TLS-SRP support: enabled resolver: POSIX threaded IPv6 support: enabled Unix sockets support: enabled IDN support: no (--with-{libidn2,winidn}) Build libcurl: Shared=no, Static=yes Built-in manual: enabled --libcurl option: enabled (--disable-libcurl-option) Verbose errors: enabled (--disable-verbose) SSPI support: no (--enable-sspi) ca cert bundle: no ca cert path: /etc/ssl/certs/ ca fallback: no LDAP support: no (--enable-ldap / --with-ldap-lib / --with-lber-lib) LDAPS support: no (--enable-ldaps) RTSP support: no (--enable-rtsp) RTMP support: no (--with-librtmp) metalink support: no (--with-libmetalink) PSL support: no (libpsl not found) HTTP2 support: disabled (--with-nghttp2) Protocols: FILE GOPHER HTTP HTTPS SMB SMBS
make curl_LDFLAGS=-all-static make install curl_LDFLAGS=-all-static
3.2靜態庫和動態庫都編譯
CPPFLAGS="-I/home/linux/opt/openssl/ -I/home/linux/opt/openssl/include" LDFLAGS="-L/home/linux/opt/openssl/lib" LIBS="-ldl" ./configure --with-ssl --enable-shared --enable-static --disable-dict --disable-ftp --disable-imap --disable-ldap --disable-ldaps --disable-pop3 --disable-proxy --disable-rtsp --disable-smtp --disable-telnet --disable-tftp --disable-zlib --without-ca-bundle --without-gnutls --without-libidn --without-librtmp --without-libssh2 --without-nss --without-zlib --prefix=/home/linux/opt/curl然後執行make&make install
configure: Configured to build curl/libcurl: curl version: 7.57.0 Host setup: i686-pc-linux-gnu Install prefix: /home/linux/opt/curl Compiler: gcc SSL support: enabled (OpenSSL) SSH support: no (--with-libssh2) zlib support: no (--with-zlib) brotli support: no (--with-brotli) GSS-API support: no (--with-gssapi) TLS-SRP support: enabled resolver: POSIX threaded IPv6 support: enabled Unix sockets support: enabled IDN support: no (--with-{libidn2,winidn}) Build libcurl: Shared=yes, Static=yes Built-in manual: enabled --libcurl option: enabled (--disable-libcurl-option) Verbose errors: enabled (--disable-verbose) SSPI support: no (--enable-sspi) ca cert bundle: no ca cert path: /etc/ssl/certs/ ca fallback: no LDAP support: no (--enable-ldap / --with-ldap-lib / --with-lber-lib) LDAPS support: no (--enable-ldaps) RTSP support: no (--enable-rtsp) RTMP support: no (--with-librtmp) metalink support: no (--with-libmetalink) PSL support: no (libpsl not found) HTTP2 support: disabled (--with-nghttp2) Protocols: FILE GOPHER HTTP HTTPS SMB SMBS
make make install
4、arm的交叉編譯
需要注意的是如果需要使用openssl,openssl的庫也需要是arm的,否則make階段會報錯。
configure: Configured to build curl/libcurl: curl version: 7.57.0 Host setup: arm-unknown-linux-gnu Install prefix: /home/linux/arm/curl Compiler: arm-linux-gcc SSL support: enabled (OpenSSL) SSH support: no (--with-libssh2) zlib support: no (--with-zlib) brotli support: no (--with-brotli) GSS-API support: no (--with-gssapi) TLS-SRP support: enabled resolver: POSIX threaded IPv6 support: enabled Unix sockets support: enabled IDN support: no (--with-{libidn2,winidn}) Build libcurl: Shared=yes, Static=yes Built-in manual: enabled --libcurl option: enabled (--disable-libcurl-option) Verbose errors: enabled (--disable-verbose) SSPI support: no (--enable-sspi) ca cert bundle: no ca cert path: no ca fallback: no LDAP support: no (--enable-ldap / --with-ldap-lib / --with-lber-lib) LDAPS support: no (--enable-ldaps) RTSP support: no (--enable-rtsp) RTMP support: no (--with-librtmp) metalink support: no (--with-libmetalink) PSL support: no (libpsl not found) HTTP2 support: disabled (--with-nghttp2) Protocols: FILE GOPHER HTTP HTTPS SMB SMBS
linux@ubuntu:~/arm/curl/lib$ cp -alibcurl.so* /nfsroot/rootfs/lib/
5、下面來幾個簡單的例子供大家測試
- #include <stdio.h>
- #include <curl/curl.h>
- #include <stdlib.h>
- int main(int argc, char *argv[])
- {
- CURL *curl; //定義CURL類型的指針
- CURLcode res; //定義CURLcode類型的變量,保存返回狀態碼
- if(argc!=2) {
- printf("Usage : file <url>;/n");
- exit(1);
- }
-
- curl_global_init(CURL_GLOBAL_ALL);
- curl = curl_easy_init(); //初始化一個CURL類型的指針
- if(curl!=NULL) {
- //設置curl選項. 其中CURLOPT_URL是讓用戶指定url. argv[1]中存放的命令行傳進來的網址
- curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
-
- //調用curl_easy_perform 執行我們的設置.並進行相關的操作. 在這裏只在屏幕上顯示出來.
- res = curl_easy_perform(curl);
- /* Check for errors */
- if(res != CURLE_OK)
- fprintf(stderr, "curl_easy_perform() failed: %s\n",
- curl_easy_strerror(res));
- //清除curl操作.
- curl_easy_cleanup(curl);
- }
- return 0;
- }
解析輸入的http網頁,直接輸出到終端
- // 採用CURLOPT_WRITEFUNCTION 實現網頁下載保存功能
- //save_http www.baidu.com /tmp/baidu
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
-
- #include <curl/curl.h>
- #include <curl/typecheck-gcc.h>
- #include <curl/easy.h>
-
- FILE *fp; //定義FILE類型指針
- //這個函數是爲了符合CURLOPT_WRITEFUNCTION而構造的
- //完成數據保存功能
- size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
- {
- int written = fwrite(ptr, size, nmemb, (FILE *)fp);
- return written;
- }
-
- int main(int argc, char *argv[])
- {
- CURL *curl;
-
- curl_global_init(CURL_GLOBAL_ALL);
- curl=curl_easy_init();
- curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
-
- if((fp=fopen(argv[2],"w"))==NULL)
- {
- curl_easy_cleanup(curl);
- exit(1);
- }
- ////CURLOPT_WRITEFUNCTION 將後繼的動作交給write_data函數處理
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
- curl_easy_perform(curl);
- curl_easy_cleanup(curl);
- exit(0);
- }
解析http並保存爲文件。