vs2017編譯並配置libcurl入門教程


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM