虚拟机环境
ubuntu12.04
开发板
EasyARM-i.MX280A: 64m sdram 128M nandflash 运行官方提供的Linux-2.6.35.3内核linux
首先,如果需要使用curl解析https需要先编译openssl库,编译这个库可以参考
http://blog.youkuaiyun.com/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并保存为文件。