curl编译以及arm交叉编译

àì夳堔傛蜴生んèń 2022-06-03 03:21 995阅读 0赞

虚拟机环境
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、下载源码

https://curl.haxx.se/download.html

在这里下载源码,我下载的是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需要自行编译,并且制定库路径。

实测使用静态库与动态库程序执行时间相差无几,所以建议使用动态库。

发表评论

表情:
评论列表 (有 0 条评论,995人围观)

还没有评论,来说两句吧...

相关阅读

    相关 go交叉编译arm64

    文章目录 go交叉编译arm64 go交叉编译arm64 golang一份代码可以编译出在不同系统和cpu架构运行的二进制文件。go也提供了很多环境变量,我

    相关 iconv的arm交叉编译

    我在使用iconv函数时出现一些问题,就是在pc上测试是好用的,但是编译到arm板子上转换出来的字符就是空。 在网上查找结果应该是交叉编译链中的libc库有些问题,所以就想到