分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

vs2017编译并配置libcurl入门教程

发布时间:2023-09-06 01:58责任编辑:沈小雨关键词:url配置编译

libcurl可以用来发送http请求,是c/c++发送http请求常用的库

下载libcurl源码包:

libcurl下载地址:https://curl.haxx.se/download.html

解压到
C:\source\repos\libcurl\curl-7.60.0

打开curl文件夹,以管理员权限运行buildconf.bat。

编译libcurl源码

选择【开始】->[Visual Studio 2017]->[Visual Studio Tools]->[VC]->[x64 Native Tools Command Prompt for VS 2017]

打开后,进入得到对应的curl目录下

cd C:\source\repos\libcurl\curl-7.60.0\winbuild

VS2017 x64 静态编译

输入下面的命令,然后回车,开始编译

nmake /f Makefile.vc mode=static VC=14 MACHINE=x64 DEBUG=no

编译选项说明:
如果想使用动态编译,将mode=static改为mode=dll。(本文仅教静态编译,同时curl官方也不建议使用动态编译)
如果使用x86,将MACHINE=x64改为MACHINE=x86。
如果需要debug版,将DEBUG=no改为DEBUG=yes。
更详细的编译指令及说明可以打开winbuild文件夹中的BUILD.WINDOWS.txt查看。

大概几分钟就可以编译结束(i5-3470 CPU 3.2GHz, 8G内存,7200转机械硬盘)。

待编译结束,关闭控制台界面。
打开C:\source\repos\libcurl\curl-7.60.0\文件夹中的builds文件夹,将名字最短的文件夹备份(如果x64和x86都编译了,则需要备份两个名字最短的文件夹),curl文件夹如果无其他需要则可全部删除。

配置VS2017工程

拷贝上面生成的C:\source\repos\libcurl\curl-7.60.0\builds\libcurl-vc15-x64-release-static-ipv6-sspi-winssl下的include和lib目录到C:\根目录下

1.打开VS2017,新建一个工程testcurl,Select [File]->[NewProject]->[Visual C++]->[Windows Desktop]->[Windows Console Application]

2.右击工程,选择属性,选择[Configurations]选择Release

3.[Platform]选择x64,确认编译时选中的也是Release, x64

4.将刚刚编译生成的文件夹中的include文件夹和lib文件夹添加至工程。(如果编译了debug版libcurl,则应将debug文件夹中的内容添加至debug配置工程)

[Configuration Properties]->[VC++ Directories],选择edit

4.1[Include Directories]里面新增C:\include

4.2[Library Directories]里面新增C:\lib

5.使用了静态编译libcurl,所以需要将CURL_STATICLIB添加至工程

[Configuration Properties]->[C/C++]->[Preprocessor],选择edit
新增CURL_STATICLIB;

6.确认[Configuration Properties]->[C/C++]->[Code Generation]->[Runtime Library]设置为“Multi-threaded DLL(/MD)”
如果没有编译debug版libcurl,则需要将“Runtime Library”改为Release版(即后面不带小写字母d)。同时官方并不建议使用“/MT”或“/MTd”。

7.[Configuration Properties]->[Additional Dependencies]里面新增libcurl_a.lib

8.输入下面由Postman生成的测试代码,Postman是个很好用的调试http接口的工具,建议简单学习一下:

#include "stdafx.h"// 需要下面的库才能通过build,具体为什么需要,以后再研究吧#pragma comment ( lib, "ws2_32.lib" )#pragma comment ( lib, "wldap32.lib" )#pragma comment(lib, "Crypt32.lib")#include <curl/curl.h> ???#include <stdio.h>using namespace std;int main(void){ ???????// 初始化 ???CURL *hnd = curl_easy_init(); ???curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST"); ???curl_easy_setopt(hnd, CURLOPT_URL, "http://193.28.51.120:8002/api/account/validtoken"); ???struct curl_slist *headers = NULL; ???headers = curl_slist_append(headers, "Authorization: Bearer I am a login token"); ???headers = curl_slist_append(headers, "Content-Type: application/json"); ???headers = curl_slist_append(headers, "accept: application/json"); ???curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers); ???curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "{\"userId\": \"43000010\"}"); ???CURLcode ret = curl_easy_perform(hnd); ???printf("ret0:%d\n", ret); ???LONG nHttpCode = 0; ???// 取下HTTP返回状态码(200为成功) ???ret = curl_easy_getinfo(hnd, CURLINFO_RESPONSE_CODE, &nHttpCode); ???printf("ret1:%d\n", ret); ???printf("nHttpCode:%d\n", nHttpCode); ???????????????// 回收资源 ???????curl_easy_cleanup(hnd); ???????return 0;}

参考:
https://blog.csdn.net/cym1990/article/details/79851039

https://blog.csdn.net/DaSo_CSDN/article/details/77587916

vs2017编译并配置libcurl入门教程

原文地址:https://www.cnblogs.com/drfxiaoliuzi/p/9146282.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved