• vs2017编译并配置libcurl入门教程


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

    下载libcurl源码包:

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

    解压到
    C:source eposlibcurlcurl-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
    eposlibcurlcurl-7.60.0winbuild
    

    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 eposlibcurlcurl-7.60.0文件夹中的builds文件夹,将名字最短的文件夹备份(如果x64和x86都编译了,则需要备份两个名字最短的文件夹),curl文件夹如果无其他需要则可全部删除。

    配置VS2017工程

    拷贝上面生成的C:source eposlibcurlcurl-7.60.0uildslibcurl-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
    ", ret);
    	LONG nHttpCode = 0;
    	// 取下HTTP返回状态码(200为成功)
    	ret = curl_easy_getinfo(hnd, CURLINFO_RESPONSE_CODE, &nHttpCode);
    	printf("ret1:%d
    ", ret);
    	printf("nHttpCode:%d
    ", nHttpCode);
            
            // 回收资源
            curl_easy_cleanup(hnd);
        
    	return 0;
    }
    
    

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

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

    转载本Blog文章请注明出处,否则,本作者保留追究其法律责任的权利。 本人转载别人或者copy别人的博客内容的部分,会尽量附上原文出处,仅供学习交流之用,如有侵权,联系立删。
  • 相关阅读:
    20210907
    彻底解决Manjaro中的编程字体问题
    manjaro上安装腾讯会议
    应对github的新变换,更加方便应用github
    Failed to stop iptables.service: Unit iptables.service not loaded.
    centOS7给虚拟机设置固定ip地址
    centOS给虚拟机设置固定ip地址
    SpringBoot启动报错Failed to determine a suitable driver class
    Jasypt加解密
    java json字符串转JSONObject对象、转JAVA对象、转List<T>对象
  • 原文地址:https://www.cnblogs.com/drfxiaoliuzi/p/9146282.html
Copyright © 2020-2023  润新知