当前位置:网站首页>win10用cmake3.22与vs2019编译curl库源码并调用
win10用cmake3.22与vs2019编译curl库源码并调用
2022-06-28 16:47:00 【万里鹏程转瞬至】
curl是一个利用URL语法在命令行下工作的文件传输工具,支持很多种http请求操作,详情可以参考Linux curl命令最全详解_Angel_CG的博客-CSDN博客_curl命令。curl现在在linux与win10都是有内置的,在命令行中可以直接使用curl指令。博主一开始找了很久,都没有看到适用于vs的curl win10库,因此自己找到了curl库的源码进行了编译,并通过了命令行调用与c++调用。
curl库的源码下载地址:https://github.com/curl/curl
博主编译环境:vs2019,cmake3.22 (编译过程中没有任何特殊设置,因此博主决定其他vs、cnake版本应该也是完全可以的)
1、下载并解压源码
在github下载后的zip包解压后得到以下目录结构,并在以下目录中创建build目录(用于后续的cmake)。

2、cmake生成vs项目
打开cmake,将源代码路径与build路径填入到下图相应的位置。然后依次点击Configure(该过程会弹出vs选项,选默认值即可)、Generate、Open Project。点击Open Project后将会在vs中打开项目。

3、vs生成库文件
此时VS的界面如下图所示,先安装自己的需求选择Release/Debug ,x64/32等版本。然后,点击选择ALL_BUILD项目,并单击右键在弹出界面中点击生成。最后,项目生成成功。输出结果可见下图最底部的红框。

进入build目录,此时生成的文件目录如下所示 。
生成的lib与dll如下图所示

生成的curl.exe如下图所示,其中红框部分是博主从lib/Release拷贝来的。因为执行curl.exe需要调用libcurl.dll。

4、curl.exe测试
与系统自带的curl相比,博主编译的curl.exe很明显不支持https,ftps等带安全认证的协议,相比是博主在编译时没有配置openssl所导致的。

5、libcurl测试
第一步 新建vs c++空项目
第二步 配置vs项目
在相应的位置配置include、lib的路径。并在链接器-》输入-》附加依赖项中配置libcurl_imp.lib;

第三步 编写C++测试代码
测试代码如下所示
#include <iostream>
#include <string>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <curl/curl.h>
using namespace std;
bool read_file(const char* path, const char* local_file)
{
cout << "download " << path << " to local: " << local_file << endl;
string url = path;
CURL* conn = curl_easy_init();
if (conn == NULL) {
fprintf(stderr, "Failed to create CURL connection\n");
return false;
}
std::cout << "down file from:" << url << std::endl;
curl_easy_setopt(conn, CURLOPT_URL, url.c_str());
//设置不验证https的ssl证书
curl_easy_setopt(conn, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(conn, CURLOPT_SSL_VERIFYHOST, 0)
;
curl_easy_setopt(conn, CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);
curl_easy_setopt(conn, CURLOPT_USERNAME, "");
curl_easy_setopt(conn, CURLOPT_PASSWORD, "");
curl_easy_setopt(conn, CURLOPT_COOKIE, "cookiejar.txt");//设置cookie 【发生到服务器】
curl_easy_setopt(conn, CURLOPT_COOKIEFILE, "cookiejar.txt");//设置cookie 【服务器操作cookie后保存】
//curl_easy_setopt(conn, CURLOPT_NOPROGRESS, 1L);
//curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, write_data);
FILE* target = fopen(local_file, "wb");
if (target == NULL) {
fprintf(stderr, "Failed to open [%s], errno: %d\n", local_file, errno);
return false;
}
curl_easy_setopt(conn, CURLOPT_WRITEDATA, target);
CURLcode code = curl_easy_perform(conn);
if (code != CURLE_OK) {
fprintf(stderr, "Failed to get file [%s]\n", curl_easy_strerror(code));
return false;
}
fclose(target);
curl_easy_cleanup(conn);
return true;
}
int main() {
read_file("https://codeload.github.com/curl/curl/zip/refs/heads/master","curl.zip");
return 0;
}
代码执行结果如下图所示,需要注意的是dll文件要与exe在同一个路径下,或者将dll文件的路径添加到系统环境变量path中。

边栏推荐
猜你喜欢

MySQL高可用之MHA(一不小心和我的青春擦肩而过)

Redis持久化(少年一貫快馬揚帆,道阻且長不轉彎)

"Popular science leaders say" intelligent bionic robot fish

Cross cluster deployment of helm applications using karmada

此虚拟机的处理器所支持的功能不同于保存虚拟机状态的虚拟机的处理器所支持的功能

PotPlayer播放百度云盘视频

Batch modify specified character file name bat script

彻底凉了!腾讯知名软件全线下架,网友一片唏嘘。。。

基于DataWorks的时效仿真平台|得物技术

rsync 远程同步(日落尤其温柔的人间皆是浪漫)
随机推荐
What is the reason why easycvr can't watch the device video when it is connected to the home protocol?
In rhel6.4, cacti+spine monitoring host is used to send email alarm
Discrete mathematics single shot, full shot and double shot
Research on master's thesis writing
Which securities company is better and safer to choose for opening an exchange fund account by mobile phone
Apache setting timeout parameter
【TcaplusDB知识库】WebClient用户如何读取和修改数据
IP problem occurs when a virtual system is cloned in VirtualBox
Redis持久化(少年一贯快马扬帆,道阻且长不转弯)
How to put your WordPress website in maintenance mode
[tcapulusdb knowledge base] batch copy the game area
【刷题日记】和为 K 的子数组
Metaq installation deployment document
Potplayer play Baidu Cloud disk video
Visubit "ai+3d vision" product series | loading assembly workstation
Fs2k face sketch attribute recognition
Batchnorm2d principle, function and explanation of batchnorm2d function parameters in pytorch
NoSQL redis configuration and optimization (I blew the night breeze of Qinhuai River for you when you were not in Nanjing)
Summary and Thinking on interface test automation
"Popular science leaders say" intelligent bionic robot fish