当前位置:网站首页>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中。

边栏推荐
- 使用Karmada实现Helm应用的跨集群部署
- PotPlayer播放百度云盘视频
- 基于Krack的网络攻击「建议收藏」
- Logback log output format
- Solve the problem of sqoop error manager SqlManager: Generic SqlManager. listDatabases() not implemented
- 如何将你的 WordPress 网站置于维护模式
- Tacacs+ server deployment
- Redis持久化(少年一贯快马扬帆,道阻且长不转弯)
- RHEL6.4中使用Cacti+Spine监控远程主机
- Tidb v6.0.0 (DMR): initial test of cache table - tidb Book rush
猜你喜欢

How to do a good job of gateway high availability protection in the big promotion scenario

Log management in MySQL log backup and recovery

关于接口测试自动化的总结与思考

本地部署Confluence遇到的问题:MySQL数据库编码、隔离级别、验证码不显示

【TcaplusDB】祝大家端午安康!

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

Redis主从复制、哨兵、cluster集群原理+实验(好好等,会晚些,但会更好)

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

NoSQL redis configuration and optimization (I blew the night breeze of Qinhuai River for you when you were not in Nanjing)

MySQL中的日志管理 日志备份与恢复
随机推荐
Q: how bad can a programmer be?
55. maximum sum of continuous subarrays
[tcapulusdb knowledge base] how webclient users read and modify data
PotPlayer播放百度雲盤視頻
Redis persistence (les adolescents naviguent toujours rapidement, obstruent et ne tournent jamais)
天翼云Web应用防火墙(边缘云版)通过首批可信认证
Redis master-slave replication, sentinel, cluster cluster principle + experiment (wait, it will be later, but it will be better)
This simple little function saves 213 hours for our production research team in half a year
VirtualBox中克隆了一个虚拟系统出现IP问题
【每日3题(1)】字符串中第二大的数字
基于DataWorks的时效仿真平台|得物技术
如何备份 WordPress 数据库
[tcaplusdb knowledge base] view tcapdir directory server
Problems encountered in local deployment conflict: MySQL database code, isolation level and verification code are not displayed
【刷题日记】和为 K 的子数组
清华大佬耗时3天总结出的'常见网络协议汇总'
[question skimming diary] and a subarray of K
Fs2k face sketch attribute recognition
"Popular science leaders say" intelligent bionic robot fish
MySQL高可用之MHA(一不小心和我的青春擦肩而過)