【Linux】编译libcurl,添加openssl和zlib支持
•C++
2438
0
新建目录~/dev/compile
,所有编译过程均在该目录下进行。
mkdir -p ~/dev/compile
cd ~/dev/compile
官网:https://zlib.net
版本:1.2.13
cd ~/dev/compile
wget https://zlib.net/zlib-1.2.13.tar.gz
tar -xzf zlib-1.2.13.tar.gz
cd zlib-1.2.13
编译【静态库】。
./configure --static --prefix=/usr/local/zlib
make
sudo make install
# 头文件
# /usr/include/zlib.h
# /usr/include/zconf.h
ls -l /usr/include | grep -e zlib.h -e zconf.h
# 静态库文件
# /usr/lib/libz.a
ls -l /usr/lib | grep libz.a
官网:https://www.openssl.org
版本:1.1.1q
cd ~/dev/compile
wget https://www.openssl.org/source/old/1.1.1/openssl-1.1.1q.tar.gz
tar -xzf openssl-1.1.1q.tar.gz
cd openssl-1.1.1q
# 创建可执行文件链接
sudo ln -s /usr/local/ssl/bin/openssl /usr/local/bin/openssl
编译【静态库】。
./config -fPIC no-shared zlib --openssldir=/usr/local/ssl --prefix=/usr/local/ssl
make -j4
sudo make install
选项说明:
官网:https://curl.se/
版本:7.85
cd ~/dev/compile
wget --no-check-certificate https://curl.se/download/curl-7.85.0.tar.gz
tar -xzf curl-7.85.0.tar.gz
cd ~/dev/compile/curl-7.85.0
编译【静态库】
./configure --prefix=/usr/local/curl --with-openssl=/usr/local/ssl --with-zlib=/usr/local/zlib --disable-shared
make -j4
sudo make install
# 创建可执行文件链接
sudo ln -s /usr/local/curl/bin/curl /usr/local/bin/curl
# 测试
curl -I https://www.baidu.com
选项说明
安装目录/usr/local/curl
代码中使用libcurl库,编译的时候,需要同时链接libcurl.a
,libz.a
,libssl.a
,libcrypto.a
4个库。
可以将这4个库文件合并为1个libcurl.a
。
# 创建临时文件夹
cd /usr/local/curl/lib
sudo mkdir merge_lib
cd merge_lib
# 复制4个库文件到这里
sudo cp /usr/local/zlib/lib/libz.a /usr/local/ssl/lib/libcrypto.a /usr/local/ssl/lib/libssl.a /usr/local/curl/lib/libcurl.a .
# 展开所有文件
sudo ar x libz.a
sudo ar x libssl.a
sudo ar x libcrypto.a
sudo ar x libcurl.a
# 合并库文件
sudo ar r libcurl_ssl_zlib.a *.o
# 备份原来的libcurl.a文件,替换
sudo mv ../libcurl.a ../libcurl_old.a
sudo mv libcurl_ssl_zlib.a ../libcurl.a
cd ..
ls -l
cd ~/dev/compile/curl-7.85.0
mkdir test_libcurl
cd test_libcurl
vim main.c
C语言测试代码如下
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <curl/curl.h>
struct memory {
char* response;
size_t size;
};
static size_t callback(void *data, size_t size, size_t nmemb, void *userp) {
size_t realsize = size * nmemb;
struct memory *mem = (struct memory *)userp;
char *ptr = (char*)realloc((void*)mem->response, mem->size + realsize + 1);
if(ptr == NULL) {
return 0; /* out of memory! */
}
mem->response = ptr;
memcpy(&(mem->response[mem->size]), data, realsize);
mem->size += realsize;
mem->response[mem->size] = '\0';
return realsize;
}
int main() {
/* 1. 初始化 */
CURL* curl = curl_easy_init();
if (!curl) {
printf("curl_easy_init failed\n");
return 1;
}
struct memory chunk = {0};
chunk.response = NULL;
chunk.size = 0;
/* 2. 发送请求 */
curl_easy_setopt(curl, CURLOPT_URL, "https://www.baidu.com");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&chunk);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_perform(curl);
/* 3. 查看请求返回结果 */
printf("%s\n", chunk.response);
/* 4. 清理 */
if (chunk.response) {
free(chunk.response);
chunk.response = NULL;
chunk.size = 0;
}
curl_easy_cleanup(curl);
curl = NULL;
return 0;
}
编译测试代码:
gcc -I/usr/local/curl/include -o out main.c -L/usr/local/curl/lib -lcurl -lpthread -ldl
运行程序:
./out
输出百度首页的html说明测试通过。