curl移植和简单使用
1.官网下载最新的源码
地址:https://curl.haxx.se/download.html
解压 tar -xf curl-7.73.0.tar.gz
cd 进入 curl-7.73.0
①移植在pc linux
执行
./configure --prefix=$PWD/_install
make && make install
②移植arm的linux
./configure --with-ssl=xxx/openssl-3.0.0-alpha9/_install/ --without-zlib LIBS="-lssl -lcrypto -lrt -ldl" --prefix=$PWD/_install --host=arm-_xxxxx CC=xxx/arm-xxx-gcc CXX=xxx/arm-xxx-g++
make && make install
make_all.sh
funMake()
{
local GCC_TEMP_PATH=
local SSL_TEMP_PATH=${PWD}/../openssl-3.0.0-alpha9/_install
local PLATFORM_TEMP=arm-linux
./configure --with-ssl=${SSL_TEMP_PATH} --without-zlib LIBS="-lssl -lcrypto -lrt -ldl" --prefix=${PWD}/_install --host=${PLATFORM_TEMP} CC=${GCC_TEMP_PATH}${PLATFORM_TEMP}-gcc
make && make install
}
funMake
--with-ssl 要支持https则需要,要安装ssl并且_install 就是编译后的库文件lib和头文件include
--prefix 安装路径
--without-zlib 不编译zlib
--host 使用的平台
CC 交叉编译器
Host setup: arm-unknown-linux-gnu
Install prefix: /root/Desktop/work/curl/curl-7.73.0/_install
Compiler: arm-linux-gcc
CFLAGS: -Werror-implicit-function-declaration -O2 -Wno-system-headers -pthread
CPPFLAGS: -isystem /root/Desktop/work/curl/curl-7.73.0/../openssl-3.0.0-alpha11/_install/include -isystem /root/Desktop/work/curl/curl-7.73.0/../openssl-3.0.0-alpha11/_install/include/openssl -DOPENSSL_SUPPRESS_DEPRECATED
LDFLAGS: -L/root/Desktop/work/curl/curl-7.73.0/../openssl-3.0.0-alpha11/_install/lib
LIBS: -lssl -lcrypto -lssl -lcrypto -lrt -ldl
curl version: 7.73.0
SSL: enabled (OpenSSL v3+)
SSH: no (--with-{libssh,libssh2})
zlib: no (--with-zlib)
brotli: no (--with-brotli)
zstd: no (--with-zstd)
GSS-API: no (--with-gssapi)
TLS-SRP: enabled
resolver: POSIX threaded
IPv6: enabled
Unix sockets: enabled
IDN: 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)
Code coverage: disabled
SSPI: no (--enable-sspi)
ca cert bundle: no
ca cert path: no
ca fallback: no
LDAP: no (--enable-ldap / --with-ldap-lib / --with-lber-lib)
LDAPS: no (--enable-ldaps)
RTSP: enabled
RTMP: no (--with-librtmp)
Metalink: no (--with-libmetalink)
PSL: no (libpsl not found)
Alt-svc: no (--enable-alt-svc)
HTTP2: no (--with-nghttp2)
HTTP3: no (--with-ngtcp2, --with-quiche)
ECH: no (--enable-ech)
Protocols: DICT FILE FTP FTPS GOPHER HTTP HTTPS IMAP IMAPS MQTT POP3 POP3S RTSP SMB SMBS SMTP SMTPS TELNET TFTP
Features: AsynchDNS HTTPS-proxy IPv6 NTLM NTLM_WB SSL TLS-SRP UnixSockets
CPPFLAGS、LDFLAGS、LIBS对应的链接路径,单我们不在--with-ssl 直接引用路径的时候要,CPPFLAGS="-Ixxx" LDFLAGS="-Lxxx"是否(enable\no)使能对的功能
编译后生成
头文件在include 库在lib上
将文件移植到开发板的上
动态库的链接路径生效 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:路径/lib
bin的curl生效 export PATH=$PATH:路径/bin
查看./cuil -V
第一行是curl版本号,对应的平台arm和编译的支持的OpenSSL 版本号
Features 有SSL说明编译成功支持https
测试 curl http://www.baidu.com 或者https://www.baidu.com看到下载文件
2.简单的使用curl
//回调函数
size_t body_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
strcat((char *)stream,(char *)ptr);
return size * nmemb;
}
//回调函数
size_t header_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
strcat((char *)stream,(char *)ptr);
return size * nmemb;
}
int main()
{
CURL *curl;
CURLcode res=0;
static char str[1024*50]={0}; // 百度页面
static char h_str[1024*50]={0}; // header
//HTTP报文头
struct curl_slist* headers = NULL;
char *url = "http://www.baidu.com";
curl = curl_easy_init();
if(curl)
{
//设置url
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_TIMEOUT,5); // 设置超时5s
//headers =curl_slist_append(headers, "Content-Type:application/json");
//curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
//curl_easy_setopt(curl, CURLOPT_POST, 1);//设置为非0表示本次操作为POST
//char *p_body="{/"test/":11}";
// 设置要POST的JSON数据
//curl_easy_setopt(curl, CURLOPT_POSTFIELDS, p_body);
//curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(p_body));//设置上传json串长度
// 设置接收数据的处理函数和存放变量
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, (void *)body_data);//设置回调函数
curl_easy_setopt(curl, CURLOPT_WRITEDATA, str);//设置写数据
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, (void *)header_data);//设置回调函数
curl_easy_setopt(curl, CURLOPT_HEADERDATA, h_str);//设置写数据
res = curl_easy_perform(curl);//执行
//curl_slist_free_all(headers); /* free the list again */
printf("\r\n------header----\r\n:%s\r\n",h_str);
printf("\r\n------body----\r\n:%s\r\n",str);
/*always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
2.openssl 移植
下载:https://www.openssl.org/ 进去官网下载openssl-3.0.0-alpha9.tar.gz
解压:tar -xf openssl-3.0.0-alpha9.tar.gz
编译:
./config no-asm linux-armv4 --prefix=$PWD/_install --openssldir=$PWD/_install CROSS_COMPILE=xxx/arm-linux-gnueabi-
make && make install
make_all.sh
funMake()
{
./config no-asm linux-armv4 --prefix=$PWD/_install --openssldir=$PWD/_install CROSS_COMPILE=arm-linux-
make && make install
}
funMake
注意:交叉编译后记得加linux-armv4 对应的架构,对应下面的第一行
Configuring OpenSSL version 3.0.0-alpha11 for target linux-armv4
Using os-specific seed configuration
Creating configdata.pm
Running configdata.pm
Creating Makefile
**********************************************************************
*** ***
*** OpenSSL has been successfully configured ***
*** ***
*** If you encounter a problem while building, please open an ***
*** issue on GitHub <https://github.com/openssl/openssl/issues> ***
*** and include the output from the following command: ***
*** ***
*** perl configdata.pm --dump ***
*** ***
*** (If you are new to OpenSSL, you might want to consult the ***
*** 'Troubleshooting' section in the INSTALL.md file first) ***
*** ***
**********************************************************************
编译成功后对应的库在_install文件夹上
查看库的信息
可以看到对应支持的平台信息和版本号