使用mingw64编译支持ssl的curl
工具下载及安装
- msys2 下载地址-> http://repo.msys2.org/distrib/x86_64/msys2-x86_64-20180531.exe
- mingw64 下载地址-> http://www.mingw-w64.org/doku.php/download/mingw-builds
- openssl 下载地址-> https://www.openssl.org/source/openssl-1.1.1-pre8.tar.gz
- libcurl 下载地址-> https://curl.haxx.se/download/curl-7.60.0.tar.gz
安装下载的mingw64和msys2,将libcurl及openssl解压到D盘根目录。
编译openssl
- 在msys2安装目录找到mingw64.exe,双击运行!
- 在mingw64命令行窗口将mingw64的安装目录的bin目录加入到环境变量
export PATH="your_mingw64_path/bin:$PATH"
- 切换到openssl解压目录,输入如下命令:
./Configure --prefix=$PWD/dist no-idea no-mdc2 no-rc5 shared mingw64
- 编译
mingw32-make depend && mingw32-make && mingw32-make install
编译完成后将会安装到你的openssl目录下的dist目录下,保存以备使用
编译libcurl
切换到你解压的libcurl所在目录,进入lib目录,打开Makefile.m32,修改SSL_PATH的值为你上一步的dist目录,可使用相对路径。
输入如下命令编译
mingw32-make -f Makefile.m32 SSL=1
等待编译完成!
使用编译的libcurl库
将你的libcurl/lib目录下的libcurl.a以及openssl/dist/lib目录下的libcrypto.a及libssl.a复制到你的lib文件夹
使用你的编辑器新建一个c文件,代码如下:
#include "your_libcurl_path\include\curl\curl.h"
// #include <curl/curl.h>
int main(int argc, char *argv[])
{
CURL *curl = 0;
CURLcode res;
curl = curl_easy_init();
if (curl != 0)
{
curl_easy_setopt(curl, CURLOPT_URL, "https://www.baidu.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
/* always cleanup */
curl_easy_cleanup(curl);
}
// getchar();
return 0;
}
使用如下命令编译并测试运行
gcc -I. -Iyour_libcurl_path/include -g -O2 -DCURL_STATICLIB -c $fileName && gcc -o $fileNameWithoutExt.exe $fileNameWithoutExt.o -Lyour_lib_path -lcurl -lssl -lcrypto -lwldap32 -lws2_32 && $dir$fileNameWithoutExt