關于libcurl庫
libcurl是一個跨平臺的網絡協議庫瘟忱,支持http, https, ftp, gopher, telnet, dict, file, 和ldap 協議。libcurl同樣支持HTTPS證書授權碗淌,HTTP POST, HTTP PUT, FTP 上傳, HTTP基本表單上傳,代理锣笨,cookies,和用戶認證。想要知道更多關于libcurl的介紹菲驴,可以到官網 http://curl.haxx.se/上去了解荐吵,在這里不再詳述。本文主要介紹如何在Visual Studio 2013開發(fā)環(huán)境中編譯并使用libcurl庫赊瞬。
1先煎、下載libcurl源代碼
libcurl最新源代碼下載地址為:https://curl.haxx.se/download/curl-7.67.0.zip,對應的下載頁面為:https://curl.haxx.se/download.html巧涧。下載后解壓到某個目錄下薯蝎,對應的文件目錄結構如下圖所示:
2、使用Visual Studio 2013編譯libcurl源代碼
在自己的Windows系統(tǒng)上安裝好Visual Sutido 2013之后谤绳,并且下載好libcurl的最新源代碼后占锯,就可以開始在Visual Studio 2013中編譯libcurl的源代碼了,筆者寫這篇文章時libcurl的最新版本是7.67.0缩筛,使用的是Windows10系統(tǒng)消略。
libcurl源代碼目錄下winbuild文件夾下的BUILD.WINDOWS.txt詳細描述了如何使用Visual C++編譯工具編譯、構建和安裝curl以及l(fā)ibcurl的源代碼瞎抛,我使用的是Visual Studio 2013艺演。
首先在Window10系統(tǒng)最左側的Windows菜單找到Visual Studio 2013下的【Visual Studio Tools】并打開,如下圖所示:
我在自己的Windows10系統(tǒng)下同時安裝了Visual Studio 2017和Visual Studio 2013桐臊。
打開Visual Studio Tools胎撤,可以看到對應的快捷鍵,如下圖所示:
為了使用Visual Studio 2013編譯出X86環(huán)境的libcurl庫断凶,我使用的是:VS2013 x86 本機工具命令提示伤提,當然如果想編譯成X64的庫,可以選擇VS2013 x64 本機工具命令提示或者VS2013 x64 兼容工具命令提示懒浮。
打開VS2013 x86 本機工具命令提示的快捷方式飘弧,進入libcurl源代碼的目錄,例如我的curl源代碼目錄放E:\SoftDevelop\CPlus\VCProject2013\curl-7.67.0下砚著,則輸入如下命令進入到該目錄
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC>E:
E:\>cd E:\SoftDevelop\CPlus\VCProject2013\curl-7.67.0
E:\SoftDevelop\CPlus\VCProject2013\curl-7.67.0>cd winbuild
E:\SoftDevelop\CPlus\VCProject2013\curl-7.67.0\winbuild>
關于使用VS2013 x86本機工具命令提示編譯libcurl源代碼次伶,采用的是VC的nmake命令,相當于Windows下的make工具稽穆。
官網文件如下:
Building with Visual C++
========================
Open a Visual Studio Command prompt:
Using the 'Developer Command Prompt for VS <version>' menu entry:
where version is the Visual Studio version. The developer prompt at default
uses the x86 mode. It is required to call Vcvarsall.bat to setup the prompt
for the machine type you want, using Vcvarsall.bat.
This type of command prompt may not exist in all Visual Studio versions.
For more information, check:
https://docs.microsoft.com/en-us/dotnet/framework/tools/developer-command-prompt-for-vs
https://docs.microsoft.com/en-us/cpp/build/how-to-enable-a-64-bit-visual-cpp-toolset-on-the-command-line
Using the 'VS <version> <platform> <type> Command Prompt' menu entry:
where version is the Visual Studio version, platform is e.g. x64
and type Native of Cross platform build. This type of command prompt
may not exist in all Visual Studio versions.
See also:
https://msdn.microsoft.com/en-us/library/f2ccy3wt.aspx
Once you are in the console, go to the winbuild directory in the Curl
sources:
cd curl-src\winbuild
Then you can call nmake /f Makefile.vc with the desired options (see below).
The builds will be in the top src directory, builds\ directory, in
a directory named using the options given to the nmake call.
nmake /f Makefile.vc mode=<static or dll> <options>
where <options> is one or many of:
VC=<6,7,8,9,10,11,12,14,15> - VC versions
WITH_DEVEL=<path> - Paths for the development files (SSL, zlib, etc.)
Defaults to sibbling directory deps: ../deps
Libraries can be fetched at https://windows.php.net/downloads/php-sdk/deps/
Uncompress them into the deps folder.
WITH_SSL=<dll or static> - Enable OpenSSL support, DLL or static
WITH_NGHTTP2=<dll or static> - Enable HTTP/2 support, DLL or static
WITH_MBEDTLS=<dll or static> - Enable mbedTLS support, DLL or static
WITH_CARES=<dll or static> - Enable c-ares support, DLL or static
WITH_ZLIB=<dll or static> - Enable zlib support, DLL or static
WITH_SSH2=<dll or static> - Enable libSSH2 support, DLL or static
WITH_PREFIX=<dir> - Where to install the build
ENABLE_SSPI=<yes or no> - Enable SSPI support, defaults to yes
ENABLE_IPV6=<yes or no> - Enable IPv6, defaults to yes
ENABLE_IDN=<yes or no> - Enable use of Windows IDN APIs, defaults to yes
Requires Windows Vista or later
ENABLE_WINSSL=<yes or no> - Enable native Windows SSL support, defaults to yes
GEN_PDB=<yes or no> - Generate Program Database (debug symbols for release build)
DEBUG=<yes or no> - Debug builds
MACHINE=<x86 or x64> - Target architecture (default is x86)
CARES_PATH=<path to cares> - Custom path for c-ares
MBEDTLS_PATH=<path to mbedTLS> - Custom path for mbedTLS
NGHTTP2_PATH=<path to HTTP/2> - Custom path for nghttp2
SSH2_PATH=<path to libSSH2> - Custom path for libSSH2
SSL_PATH=<path to OpenSSL> - Custom path for OpenSSL
ZLIB_PATH=<path to zlib> - Custom path for zlib
上面的文檔中冠王,可以使用DEBUG=yes編譯Debug版本的庫,默認是Release版本的舌镶;
MACHINE=x64可以編譯生成x64的庫柱彻,默認是x86的豪娜。
- 1、 使用VS2013編譯libcurl的debug版的static靜態(tài)庫 libcurl-vc-x86-debug-static-ipv6-sspi-winssl
對應的命令為:nmake /f Makefile.vc mode=static DEBUG=yes
E:\SoftDevelop\CPlus\VCProject2013\curl-7.67.0\winbuild>nmake /f Makefile.vc mode=static DEBUG=yes
- 2哟楷、使用VS2013編譯libcurl的debug本的dll動態(tài)庫 libcurl-vc-x86-debug-dll-ipv6-sspi-winssl
- 對應的命令為:nmake /f Makefile.vc mode=dll DEBUG=yes
E:\SoftDevelop\CPlus\VCProject2013\curl-7.67.0\winbuild>nmake /f Makefile.vc mode=dll DEBUG=yes
- 3瘤载、使用VS2013編譯libcurl的release版的x86的static靜態(tài)庫 libcurl-vc-x86-release-static-ipv6-sspi-winssl
命令為:nmake /f Makefile.vc mode=static
E:\SoftDevelop\CPlus\VCProject2013\curl-7.67.0\winbuild>nmake /f Makefile.vc mode=static
Microsoft (R) 程序維護實用工具 12.00.21005.1 版
版權所有 (C) Microsoft Corporation。 保留所有權利卖擅。
configuration name: libcurl-vc-x86-release-static-ipv6-sspi-winssl
cl /O2 /DNDEBUG /MD /DCURL_STATICLIB /I. /I ../lib /I../include /nologo /W4 /wd4127 /EHsc /DWIN32 /FD /c /DBUILDING_LIBCURL /DUSE_WIN32_IDN /DWANT_IDN_PROTOTYPES /DUSE_IPV6 /DUSE_WINDOWS_SSPI /DUSE_SCHANNEL /Fo"..\builds\libcurl-vc-x86-release-static-ipv6-sspi-winssl-obj-lib/file.obj" ..\lib\file.c
file.c
- 4鸣奔、使用VS2013編譯libcurl的release的dll動態(tài)庫 libcurl-vc-x86-release-dll-ipv6-sspi-winssl
對應的命令為:nmake /f Makefile.vc mode=dll
E:\SoftDevelop\CPlus\VCProject2013\curl-7.67.0\winbuild>nmake /f Makefile.vc mode=dll
默認會在源代碼的頂層目錄下創(chuàng)建builds文件夾,用于存放編譯生成的文件惩阶。
如下圖所示:
上面的目錄挎狸,以編譯debug版本的dll庫為例,最后為在curl源代碼所在目錄的builds下生成libcurl-vc-x86-debug-dll-ipv6-sspi-winssl文件夾断楷,它是編譯最終頭文件锨匆、庫文件、可執(zhí)行程序所在目錄冬筒,include是curl庫的頭文件存放目錄恐锣;lib庫是庫文件所在目錄,里面有l(wèi)ibcurl_debug.lib文件账千;bin目錄是生成的curl.exe可執(zhí)行文件和libcurl_debug.dll的dll文件所在目錄侥蒙,如下圖所示:
而libcurl-vc-x86-debug-dll-ipv6-sspi-winssl-obj-curl和libcurl-vc-x86-debug-dll-ipv6-sspi-winssl-obj-lib只是一些編譯生成的中間文件。
我們需要的就是libcurl-vc-x86-debug-dll-ipv6-sspi-winssl文件夾中的頭文件匀奏、庫文件以及dll庫鞭衩。可以根據需要選擇自己所需的版本娃善。
這里我選擇libcurl-vc-x86-debug-dll-ipv6-sspi-winssl以及l(fā)ibcurl-vc-x86-release-dll-ipv6-sspi-winssl文件夾中的庫论衍,即使用debug和release版的dll動態(tài)庫,稍作整理聚磺,形成如下的目錄結構:
卷 Data 的文件夾 PATH 列表
卷序列號為 3EE8-BA3A
D:.
│ tree.txt
│
├─bin
│ curl.exe
│ libcurl.dll
│ libcurl_debug.dll
│
├─include
│ └─curl
│ curl.h
│ curlver.h
│ easy.h
│ mprintf.h
│ multi.h
│ stdcheaders.h
│ system.h
│ typecheck-gcc.h
│ urlapi.h
│
└─lib
└─x86
├─debug
│ libcurl_debug.lib
│
└─release
libcurl.lib
為了方便坯台,我一般把一些第三方庫放在系統(tǒng)的某個固定目錄,如D:\env_build目錄瘫寝,我把編譯后的libcurl的debug和release版的x86動態(tài)庫以及頭文件存放在libcurl_vs2013_x86目錄下蜒蕾。
3、在Visual Studio 2013中使用libcurl庫
(1)焕阿、創(chuàng)建一個Win32 VC++控制臺的空項目
打開Visual Studio 2013開發(fā)工具咪啡,創(chuàng)建一個基于Win32 VC++的控制臺的空項目,取名為libcurlDemo1,并選好程序存放的路徑暮屡,我的存放路徑為撤摸,如下圖所示:
至此在Visual Studio 2013中生成了一個VC++控制臺的空項目,如下圖所示:
(2)添加項目代碼以及l(fā)ibcurl的頭文件和依賴庫
在Visual Stuido 2013中新增一個main.cpp源代碼文件,
main.cpp只是一個使用libcurl庫的示例程序准夷,更多的示例可以參考libcurl官網:https://curl.haxx.se/libcurl/c/example.html或者Githubhttps://github.com/curl/curl/tree/master/docs/examples
// main.cpp
#include <stdio.h>
#include <curl/curl.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
CURL *curl; //定義CURL類型的指針
CURLcode res; //定義CURLcode類型的變量钥飞,保存返回狀態(tài)碼
char url[] = "www.baidu.com";
curl = curl_easy_init(); //初始化一個CURL類型的指針
if (curl != NULL)
{
//設置curl選項. 其中CURLOPT_URL是讓用戶指 定url. url中存放的是網址
curl_easy_setopt(curl, CURLOPT_URL, url);
//調用curl_easy_perform 執(zhí)行我們的設置.并進行相關的操作. 在這 里只在屏幕上顯示出來.
res = curl_easy_perform(curl);
//清除curl操作.
curl_easy_cleanup(curl);
}
return 0;
}
從上圖可以看到在沒有導入libcurl庫的頭文件時VS2013會提示紅色的錯誤。
下面在VS2013中加入我們剛才編譯好的libcurl庫
在VS2013左下角的【屬性管理器】->【Debug|Win32】,右鍵單擊【Debug|Win32】衫嵌,選擇【屬性】读宙,進入到【Debug屬性頁對話框】,如下圖所示:
-
添加libcurl的頭文件所在目錄
選擇【C/C++】->【常規(guī)】->【附加包含目錄】楔绞,在【附加包含目錄】中添加libcurl所在的頭文件路徑论悴,我的是:D:\env_build\libcurl_vs2013_x86\include
如下圖所示:
- 添加libcurl的debug動態(tài)庫目錄
依次選擇【Debug屬性頁】->【鏈接器】->【常規(guī)】,在附加庫目錄中添加剛才編譯好的lib庫所在目錄墓律,如D:\env_build\libcurl_vs2013_x86\lib\x86\debug目錄,如下圖所示:
選擇【Debug屬性頁】->【鏈接器】->【輸入】->【附加依賴項】中幔亥,加入D:\env_build\libcurl_vs2013_x86\lib\x86\debug目錄下的libcurl_debug.lib文件名耻讽,如下圖所示:
當然我使用的debug x86模式的程序,release的x86也類似帕棉,x64的也是针肥,加上對應的頭文件和庫文件即可。
頭文件加入后香伴,相關的紅色錯誤消失了慰枕,如下圖所示:
3、運行l(wèi)ibcurlDemo1程序
添加好libcurl庫的頭文件和動態(tài)庫后即纲,運行程序具帮,發(fā)現出現如下的錯誤:
說是找不到libcurl_debug.dll動態(tài)庫文件,我們可以將剛才編譯生成D:\env_build\libcurl_vs2013_x86\bin目錄下的libcurl_debug.dll文件拷貝到libcurlDemo1項目生成的可執(zhí)行程序libcurlDemo1.exe所在目錄低斋,入下圖所示:
然后再次運行l(wèi)ibcurlDemo1程序蜂厅,可以看到控制臺中輸出了從www.baidu.com頁面獲取的html頁面膊畴,如下圖所示:
4掘猿、使用屬性表文件
有時候為了避免為每個項目都添加頭文件和lib依賴庫文件,可以使用為某個第三方依賴庫比如libcurl創(chuàng)建對應的屬性表文件唇跨,以props為后綴稠通,
在剛才的VS2013的libcurlDemo1項目中,針對debug的x86項目买猖,創(chuàng)建一個libcurl_debug_x86_vs2013.props的屬性表文件改橘,選擇【屬性頁管理器】->【Debug|Win32】,右鍵單擊政勃,【添加新項目屬性表】唧龄,在【添加新項】對話框中輸入屬性表名稱,如下圖所示:
右鍵單擊libcurl_debug_x86_vs2013.props的屬性表文件,選擇【屬性】菜單,在libcurl_debug_x86_vs2013.props屬性頁的對話框中添加libcurl的頭文件和lib庫文件既棺,同上讽挟。
5、相關資料
(1)build-libcurl-windows
github上面有一個項目build-libcurl-windows丸冕,Batch script to download and build libcurl (using Visual Studio compiler)耽梅,即使用build.bat腳本下載libcurl源代碼并編譯,前提是在Windows系統(tǒng)下安裝了Visual Studio開發(fā)環(huán)境胖烛,本人親測好使眼姐。
(2)、build-libcurl-ios
github上面有一個項目build-libcurl-ios佩番,是針對ios環(huán)境下的libcurl的編譯众旗。