linux curl编译 arm交叉编译

虚拟机环境
ubuntu12.04
开发板

EasyARM-i.MX280A:   64m  sdram  128M  nandflash   运行官方提供的Linux-2.6.35.3内核linux

首先,如果需要使用curl解析https需要先编译openssl库,编译这个库可以参考

《linux 交叉编译 openssl》https://blog.csdn.net/whatday/article/details/96978381

现在我们先讲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

正常是不会遇到问题,遇到问题再具体解决

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&make install
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

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
make

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++配置交叉编译器

配置执行完成后如下信息

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

需要注意的是如果需要使用openssl,openssl的库也需要是arm的,否则make阶段会报错。

然后就是make&make install

完成后将库文件复制到开发板中,运行时需要找到动态库

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并保存为文件。

注意:当使用静态库时需要额外连接几个库,否则连接出错

LIBS := curl pthread ssl crypto dl rt

其中ssl和crypto需要自行编译,并且制定库路径。

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

 

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值