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

边栏推荐
- How to make your WordPress website more secure
- 免费、强大、高颜值的笔记软件评测: OneNote、Heptabase、氢图、FlowUs
- "Jay bear" plummeted by 96.6%. Why is NFT with star goods cold?
- Which securities company is better and safer to choose for opening an exchange fund account by mobile phone
- AutoSAR 软件开发培训
- 这个简单的小功能,半年为我们产研团队省下213个小时
- [question skimming diary] and a subarray of K
- "Popular science leaders say" intelligent bionic robot fish
- MySQL高可用之MHA(一不小心和我的青春擦肩而過)
- Written interview algorithm classic - longest palindrome substring
猜你喜欢

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

提升可观测性 - 业务指标监控实践

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

12 SQL optimization schemes summarized by old drivers (very practical)

大促场景下,如何做好网关高可用防护

【TcaplusDB】祝大家端午安康!

Potplayer plays Baidu cloud disk video

Redis persistence (les adolescents naviguent toujours rapidement, obstruent et ne tournent jamais)

Summary and Thinking on interface test automation

彻底凉了!腾讯知名软件全线下架,网友一片唏嘘。。。
随机推荐
大促场景下,如何做好网关高可用防护
Written interview algorithm classic - longest palindrome substring
How to open a futures account? Where is it safer to open a futures account?
MySQL高可用之MHA(一不小心和我的青春擦肩而過)
Opencv read picture details
【离散数学】单射、满射与双射
【世界海洋日】TcaplusDB号召你一同保护海洋生物多样性
Can Huawei become a "brother of lipstick" or a "Queen of goods"?
基于DataWorks的时效仿真平台|得物技术
Lucky draw animation - Carp jumps over the dragon's gate
如何将你的 WordPress 网站置于维护模式
解决sqoop出现 ERROR manager.SqlManager: Generic SqlManager.listDatabases() not implemented
NoSQL之Redis配置与优化(你不在南京的日子我替你吹了秦淮河的晚风)
Potplayer plays Baidu cloud disk video
免费、强大、高颜值的笔记软件评测: OneNote、Heptabase、氢图、FlowUs
如何备份 WordPress 数据库
From five capabilities to "1+5+n", Huawei makes the transformation of government and enterprises more stable
offsetwidth\clientwidth\scrollwidth
[tcapulusdb knowledge base] batch copy the game area
【TcaplusDB】祝大家端午安康!