libcurl是免费的轻量级的客户端网络库,支持DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS,POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, Telnet, TFTP.支持SSL, HTTPPOST,HTTPPUT, FTP上传, HTTP form上传,代理,cookies, 用户名与密码认证。
系统环境:Ubuntu 14.04.3 LTS
源码:curl-7.49.0.tar.gz (https://curl.haxx.se/download.html)
交叉编译环境:arm-none-linux-gnueabi-
[zhaojq@virtual-machine]# tar -zxvf curl-7.49.0.tar.gz
[zhaojq@virtual-machine]# cd curl-7.49.0/
[zhaojq@virtual-machine]# ./configure --prefix=/home/zhaojq/libcurl --host=arm-none-linux CC=arm-none-linux-gnueabi-gcc CXX=arm-none-linux-gnueabi-g++
[zhaojq@virtual-machine]# make
[zhaojq@virtual-machine]# make install
生成成功
交叉编译后的文件在/home/zhaojq/libcurl目录下
[zhaojq@virtual-machine libcurl]# ls
bin include lib share
libcurl头文件在include/curl目录
[zhaojq@virtual-machine libcurl/include/curl]# ls
curlbuild.h curl.h curlrules.h curlver.h easy.h mprintf.h multi.h stdcheaders.h typecheck-gcc.h
交叉编译后的动态库文件在lib目录
[zhaojq@virtual-machine libcurl/lib]# ls
libcurl.a libcurl.la libcurl.so libcurl.so.4 libcurl.so.4.4.0 pkgconfig
编译test实例
https://curl.haxx.se/libcurl/c/example.html
post-callback
|
An example source code that issues a HTTP POST and we provide the actual data through a read callback.
|
[zhaojq@virtual-machine]# vim libcurl_test.cpp
-
#include
-
#include
-
#include
-
-
const char data[]="this is what we post to the silly web server";
-
-
struct WriteThis {
-
const char *readptr;
-
long sizeleft;
-
};
-
-
static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
-
{
-
struct WriteThis *pooh = (struct WriteThis *)userp;
-
-
if(size*nmemb < 1)
-
return 0;
-
-
if(pooh->sizeleft) {
-
*(char *)ptr = pooh->readptr[0];
-
pooh->readptr++;
-
pooh->sizeleft--;
-
return 1;
-
}
-
-
return 0;
-
}
-
-
int main(void)
-
{
-
CURL *curl;
-
CURLcode res;
-
-
struct WriteThis pooh;
-
-
pooh.readptr = data;
-
pooh.sizeleft = (long)strlen(data);
-
-
-
res = curl_global_init(CURL_GLOBAL_DEFAULT);
-
-
if(res != CURLE_OK) {
-
fprintf(stderr, "curl_global_init() failed: %s\n",
-
curl_easy_strerror(res));
-
return 1;
-
}
-
-
-
curl = curl_easy_init();
-
if(curl) {
-
-
curl_easy_setopt(curl, CURLOPT_URL, "");
-
-
-
curl_easy_setopt(curl, CURLOPT_POST, 1L);
-
-
-
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
-
-
-
curl_easy_setopt(curl, CURLOPT_READDATA, &pooh);
-
-
-
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
-
-
-
-
-
-
-
-
-
#ifdef USE_CHUNKED
-
{
-
struct curl_slist *chunk = NULL;
-
-
chunk = curl_slist_append(chunk, "Transfer-Encoding: chunked");
-
res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
-
-
-
}
-
#else
-
-
-
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, pooh.sizeleft);
-
#endif
-
-
#ifdef DISABLE_EXPECT
-
-
-
-
-
-
-
-
-
{
-
struct curl_slist *chunk = NULL;
-
-
chunk = curl_slist_append(chunk, "Expect:");
-
res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
-
-
-
}
-
#endif
-
-
-
res = curl_easy_perform(curl);
-
-
if(res != CURLE_OK)
-
fprintf(stderr, "curl_easy_perform() failed: %s\n",
-
curl_easy_strerror(res));
-
-
-
curl_easy_cleanup(curl);
-
}
-
curl_global_cleanup();
-
return 0;
-
}
[zhaojq@virtual-machine]# arm-none-linux-gnueabi-g++ -lcurl -I/home/kt/libcurl/include -L/home/kt/libcurl/lib -o libcurl_test libcurl.cpp
编译通过,在当前目录生成libcurl_test可执行文件
将/home/zhaojq/libcurl目录下的所有文件和pkgconfig目录都拷贝到ARM设备上文件系统的/lib目录,
将libcurl_test拷贝到ARM设备上
执行./libcurl_test
./libcurl_test: error while loading shared libraries: libssl.so.1.0.0: cannot open shared object file: No such file or directory
libcurl运行需要libssl.so.1.0.0的库,ARM移植openssl-1.0.0s.tar.gz详见另一篇文章
http://blog.csdn.net/miaodichiyou/article/details/50385049
移植成功再次执行./libcurl_test
[root@ARM /]# ./libcurl_test
* Trying 93.184.216.34...
* Connected to example.com (93.184.216.34) port 80 (#0)
> POST /index.cgi HTTP/1.1
Host: example.com
Accept: */*
Content-Length: 44
Content-Type: application/x-www-form-urlencoded
* We are completely uploaded and fine
< HTTP/1.1 404 Not Found
< Cache-Control: max-age=604800
< Content-Type: text/html
< Date: Thu, 19 May 2016 02:45:50 GMT
< Expires: Thu, 26 May 2016 02:45:50 GMT
< Server: EOS (lax004/2813)
< Content-Length: 460
<
"">
404 - Not Found
404 - Not Found
* Connection #0 to host example.com left intact
移植成功。
阅读(857) | 评论(0) | 转发(0) |