curl在linux下編譯和交叉編譯動態庫在ARM9上使用

虛擬機環境

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、下載源碼

在這裏下載源碼,我下載的是curl 7.57.0, Released on the 29th of November 2017.                    curl-7.57.0.tar.gz

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

正常是不會遇到問題,遇到問題再具體解決
  1. configure: Configured to build curl/libcurl:
  2. curl version: 7.57.0
  3. Host setup: i686-pc-linux-gnu
  4. Install prefix: /home/linux/opt/curl
  5. Compiler: gcc
  6. SSL support: enabled (OpenSSL)
  7. SSH support: no (--with-libssh2)
  8. zlib support: no (--with-zlib)
  9. brotli support: no (--with-brotli)
  10. GSS-API support: no (--with-gssapi)
  11. TLS-SRP support: enabled
  12. resolver: POSIX threaded
  13. IPv6 support: enabled
  14. Unix sockets support: enabled
  15. IDN support: no (--with-{libidn2,winidn})
  16. Build libcurl: Shared=no, Static=yes
  17. Built-in manual: enabled
  18. --libcurl option: enabled (--disable-libcurl-option)
  19. Verbose errors: enabled (--disable-verbose)
  20. SSPI support: no (--enable-sspi)
  21. ca cert bundle: no
  22. ca cert path: /etc/ssl/certs/
  23. ca fallback: no
  24. LDAP support: no (--enable-ldap / --with-ldap-lib / --with-lber-lib)
  25. LDAPS support: no (--enable-ldaps)
  26. RTSP support: no (--enable-rtsp)
  27. RTMP support: no (--with-librtmp)
  28. metalink support: no (--with-libmetalink)
  29. PSL support: no (libpsl not found)
  30. HTTP2 support: disabled (--with-nghttp2)
  31. Protocols: FILE GOPHER HTTP HTTPS SMB SMBS
然後執行make&make install
  1. make curl_LDFLAGS=-all-static
  2. 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
  1. configure: Configured to build curl/libcurl:
  2. curl version: 7.57.0
  3. Host setup: i686-pc-linux-gnu
  4. Install prefix: /home/linux/opt/curl
  5. Compiler: gcc
  6. SSL support: enabled (OpenSSL)
  7. SSH support: no (--with-libssh2)
  8. zlib support: no (--with-zlib)
  9. brotli support: no (--with-brotli)
  10. GSS-API support: no (--with-gssapi)
  11. TLS-SRP support: enabled
  12. resolver: POSIX threaded
  13. IPv6 support: enabled
  14. Unix sockets support: enabled
  15. IDN support: no (--with-{libidn2,winidn})
  16. Build libcurl: Shared=yes, Static=yes
  17. Built-in manual: enabled
  18. --libcurl option: enabled (--disable-libcurl-option)
  19. Verbose errors: enabled (--disable-verbose)
  20. SSPI support: no (--enable-sspi)
  21. ca cert bundle: no
  22. ca cert path: /etc/ssl/certs/
  23. ca fallback: no
  24. LDAP support: no (--enable-ldap / --with-ldap-lib / --with-lber-lib)
  25. LDAPS support: no (--enable-ldaps)
  26. RTSP support: no (--enable-rtsp)
  27. RTMP support: no (--with-librtmp)
  28. metalink support: no (--with-libmetalink)
  29. PSL support: no (libpsl not found)
  30. HTTP2 support: disabled (--with-nghttp2)
  31. Protocols: FILE GOPHER HTTP HTTPS SMB SMBS
然後執行make&make install
  1. make
  2. make install
其中
CPPFLAGS="-I/home/linux/opt/openssl/ -I/home/linux/opt/openssl/include" LDFLAGS="-L/home/linux/opt/openssl/lib"
是openssl的頭文件路徑以及openssl庫文件路徑
--prefix=/home/linux/opt/curl
是編譯好後的安裝位置
這樣就完成了PC端的編譯,編譯好的庫文件安裝在/home/linux/opt/curl/lib下。

4、arm的交叉編譯

在完成了PC端的編譯後,arm的交叉編譯就簡單很多,之前的步驟都一樣,解壓進入目錄。
輸入配置信息如下:
CPPFLAGS="-I/home/linux/arm/openssl/ -I/home/linux/arm/openssl/include" LDFLAGS="-L/home/linux/arm/openssl/lib" LIBS="-ldl" ./configure --host=arm-linux CC=arm-linux-gcc CXX=arm-linux-g++ --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/arm/curl
其中 --host=arm-linux CC=arm-linux-gcc CXX=arm-linux-g++配置交叉編譯器
配置執行完成後如下信息
  1. configure: Configured to build curl/libcurl:
  2. curl version: 7.57.0
  3. Host setup: arm-unknown-linux-gnu
  4. Install prefix: /home/linux/arm/curl
  5. Compiler: arm-linux-gcc
  6. SSL support: enabled (OpenSSL)
  7. SSH support: no (--with-libssh2)
  8. zlib support: no (--with-zlib)
  9. brotli support: no (--with-brotli)
  10. GSS-API support: no (--with-gssapi)
  11. TLS-SRP support: enabled
  12. resolver: POSIX threaded
  13. IPv6 support: enabled
  14. Unix sockets support: enabled
  15. IDN support: no (--with-{libidn2,winidn})
  16. Build libcurl: Shared=yes, Static=yes
  17. Built-in manual: enabled
  18. --libcurl option: enabled (--disable-libcurl-option)
  19. Verbose errors: enabled (--disable-verbose)
  20. SSPI support: no (--enable-sspi)
  21. ca cert bundle: no
  22. ca cert path: no
  23. ca fallback: no
  24. LDAP support: no (--enable-ldap / --with-ldap-lib / --with-lber-lib)
  25. LDAPS support: no (--enable-ldaps)
  26. RTSP support: no (--enable-rtsp)
  27. RTMP support: no (--with-librtmp)
  28. metalink support: no (--with-libmetalink)
  29. PSL support: no (libpsl not found)
  30. HTTP2 support: disabled (--with-nghttp2)
  31. Protocols: FILE GOPHER HTTP HTTPS SMB SMBS
需要注意的是如果需要使用openssl,openssl的庫也需要是arm的,否則make階段會報錯。
然後就是make&make install
完成後將庫文件複製到開發板中,運行時需要找到動態庫

linux@ubuntu:~/arm/curl/lib$ cp -alibcurl.so* /nfsroot/rootfs/lib/

5、下面來幾個簡單的例子供大家測試

  1. #include <stdio.h>
  2. #include <curl/curl.h>
  3. #include <stdlib.h>
  4. int main(int argc, char *argv[])
  5. {
  6. CURL *curl; //定義CURL類型的指針
  7. CURLcode res; //定義CURLcode類型的變量,保存返回狀態碼
  8. if(argc!=2) {
  9. printf("Usage : file <url>;/n");
  10. exit(1);
  11. }
  12. curl_global_init(CURL_GLOBAL_ALL);
  13. curl = curl_easy_init(); //初始化一個CURL類型的指針
  14. if(curl!=NULL) {
  15. //設置curl選項. 其中CURLOPT_URL是讓用戶指定url. argv[1]中存放的命令行傳進來的網址
  16. curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
  17. //調用curl_easy_perform 執行我們的設置.並進行相關的操作. 在這裏只在屏幕上顯示出來.
  18. res = curl_easy_perform(curl);
  19. /* Check for errors */
  20. if(res != CURLE_OK)
  21. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  22. curl_easy_strerror(res));
  23. //清除curl操作.
  24. curl_easy_cleanup(curl);
  25. }
  26. return 0;
  27. }
解析輸入的http網頁,直接輸出到終端
  1. // 採用CURLOPT_WRITEFUNCTION 實現網頁下載保存功能
  2. //save_http www.baidu.com /tmp/baidu
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <curl/curl.h>
  7. #include <curl/typecheck-gcc.h>
  8. #include <curl/easy.h>
  9. FILE *fp; //定義FILE類型指針
  10. //這個函數是爲了符合CURLOPT_WRITEFUNCTION而構造的
  11. //完成數據保存功能
  12. size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
  13. {
  14. int written = fwrite(ptr, size, nmemb, (FILE *)fp);
  15. return written;
  16. }
  17. int main(int argc, char *argv[])
  18. {
  19. CURL *curl;
  20. curl_global_init(CURL_GLOBAL_ALL);
  21. curl=curl_easy_init();
  22. curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
  23. if((fp=fopen(argv[2],"w"))==NULL)
  24. {
  25. curl_easy_cleanup(curl);
  26. exit(1);
  27. }
  28. ////CURLOPT_WRITEFUNCTION 將後繼的動作交給write_data函數處理
  29. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  30. curl_easy_perform(curl);
  31. curl_easy_cleanup(curl);
  32. exit(0);
  33. }
解析http並保存爲文件。

注意:當使用靜態庫時需要額外連接幾個庫,否則連接出錯
LIBS := curl pthread ssl crypto dl rt
其中ssl和crypto需要自行編譯,並且制定庫路徑。
實測使用靜態庫與動態庫程序執行時間相差無幾,所以建議使用動態庫。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章