關(guān)于libcurl庫
libcurl是一個(gè)跨平臺(tái)的網(wǎng)絡(luò)協(xié)議庫,支持http, https, ftp, gopher, telnet, dict, file, 和ldap 協(xié)議。libcurl同樣支持HTTPS證書授權(quán),HTTP POST, HTTP PUT, FTP 上傳, HTTP基本表單上傳,代理,cookies,和用戶認(rèn)證。想要知道更多關(guān)于libcurl的介紹,可以到官網(wǎng) http://curl.haxx.se/上去了解,在這里不再詳述。本文主要介紹如何在Visual Studio 2013開發(fā)環(huán)境中編譯并使用libcurl庫。
1、下載libcurl源代碼
libcurl最新源代碼下載地址為:https://curl.haxx.se/download/curl-7.67.0.zip,對應(yīng)的下載頁面為:https://curl.haxx.se/download.html。下載后解壓到某個(gè)目錄下,對應(yīng)的文件目錄結(jié)構(gòu)如下圖所示:
2、使用Visual Studio 2013編譯libcurl源代碼
在自己的Windows系統(tǒng)上安裝好Visual Sutido 2013之后,并且下載好libcurl的最新源代碼后,就可以開始在Visual Studio 2013中編譯libcurl的源代碼了,筆者寫這篇文章時(shí)libcurl的最新版本是7.67.0,使用的是Windows10系統(tǒng)。
libcurl源代碼目錄下winbuild文件夾下的BUILD.WINDOWS.txt詳細(xì)描述了如何使用Visual C++編譯工具編譯、構(gòu)建和安裝curl以及l(fā)ibcurl的源代碼,我使用的是Visual Studio 2013。
首先在Window10系統(tǒng)最左側(cè)的Windows菜單找到Visual Studio 2013下的【Visual Studio Tools】并打開,如下圖所示:
我在自己的Windows10系統(tǒng)下同時(shí)安裝了Visual Studio 2017和Visual Studio 2013。
打開Visual Studio Tools,可以看到對應(yīng)的快捷鍵,如下圖所示:
為了使用Visual Studio 2013編譯出X86環(huán)境的libcurl庫,我使用的是:VS2013 x86 本機(jī)工具命令提示,當(dāng)然如果想編譯成X64的庫,可以選擇VS2013 x64 本機(jī)工具命令提示或者VS2013 x64 兼容工具命令提示。
打開VS2013 x86 本機(jī)工具命令提示的快捷方式,進(jìn)入libcurl源代碼的目錄,例如我的curl源代碼目錄放E:\SoftDevelop\CPlus\VCProject2013\curl-7.67.0下,則輸入如下命令進(jìn)入到該目錄
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>
關(guān)于使用VS2013 x86本機(jī)工具命令提示編譯libcurl源代碼,采用的是VC的nmake命令,相當(dāng)于Windows下的make工具。
官網(wǎng)文件如下:
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版本的庫,默認(rèn)是Release版本的;
MACHINE=x64可以編譯生成x64的庫,默認(rèn)是x86的。
- 1、 使用VS2013編譯libcurl的debug版的static靜態(tài)庫 libcurl-vc-x86-debug-static-ipv6-sspi-winssl
對應(yīng)的命令為: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動(dòng)態(tài)庫 libcurl-vc-x86-debug-dll-ipv6-sspi-winssl
- 對應(yīng)的命令為: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) 程序維護(hù)實(shí)用工具 12.00.21005.1 版
版權(quán)所有 (C) Microsoft Corporation。 保留所有權(quán)利。
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動(dòng)態(tài)庫 libcurl-vc-x86-release-dll-ipv6-sspi-winssl
對應(yīng)的命令為:nmake /f Makefile.vc mode=dll
E:\SoftDevelop\CPlus\VCProject2013\curl-7.67.0\winbuild>nmake /f Makefile.vc mode=dll
默認(rèn)會(huì)在源代碼的頂層目錄下創(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庫。可以根據(jù)需要選擇自己所需的版本。
這里我選擇libcurl-vc-x86-debug-dll-ipv6-sspi-winssl以及l(fā)ibcurl-vc-x86-release-dll-ipv6-sspi-winssl文件夾中的庫,即使用debug和release版的dll動(dòng)態(tài)庫,稍作整理,形成如下的目錄結(jié)構(gòu):
卷 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)的某個(gè)固定目錄,如D:\env_build目錄,我把編譯后的libcurl的debug和release版的x86動(dòng)態(tài)庫以及頭文件存放在libcurl_vs2013_x86目錄下。
3、在Visual Studio 2013中使用libcurl庫
(1)、創(chuàng)建一個(gè)Win32 VC++控制臺(tái)的空項(xiàng)目
打開Visual Studio 2013開發(fā)工具,創(chuàng)建一個(gè)基于Win32 VC++的控制臺(tái)的空項(xiàng)目,取名為libcurlDemo1,并選好程序存放的路徑,我的存放路徑為,如下圖所示:
至此在Visual Studio 2013中生成了一個(gè)VC++控制臺(tái)的空項(xiàng)目,如下圖所示:
(2)添加項(xiàng)目代碼以及l(fā)ibcurl的頭文件和依賴庫
在Visual Stuido 2013中新增一個(gè)main.cpp源代碼文件,
main.cpp只是一個(gè)使用libcurl庫的示例程序,更多的示例可以參考libcurl官網(wǎng):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(); //初始化一個(gè)CURL類型的指針
if (curl != NULL)
{
//設(shè)置curl選項(xiàng). 其中CURLOPT_URL是讓用戶指 定url. url中存放的是網(wǎng)址
curl_easy_setopt(curl, CURLOPT_URL, url);
//調(diào)用curl_easy_perform 執(zhí)行我們的設(shè)置.并進(jìn)行相關(guān)的操作. 在這 里只在屏幕上顯示出來.
res = curl_easy_perform(curl);
//清除curl操作.
curl_easy_cleanup(curl);
}
return 0;
}
從上圖可以看到在沒有導(dǎo)入libcurl庫的頭文件時(shí)VS2013會(huì)提示紅色的錯(cuò)誤。
下面在VS2013中加入我們剛才編譯好的libcurl庫
在VS2013左下角的【屬性管理器】->【Debug|Win32】,右鍵單擊【Debug|Win32】,選擇【屬性】,進(jìn)入到【Debug屬性頁對話框】,如下圖所示:
-
添加libcurl的頭文件所在目錄
選擇【C/C++】->【常規(guī)】->【附加包含目錄】,在【附加包含目錄】中添加libcurl所在的頭文件路徑,我的是:D:\env_build\libcurl_vs2013_x86\include
如下圖所示:
18 - 添加libcurl的debug動(dòng)態(tài)庫目錄
依次選擇【Debug屬性頁】->【鏈接器】->【常規(guī)】,在附加庫目錄中添加剛才編譯好的lib庫所在目錄,如D:\env_build\libcurl_vs2013_x86\lib\x86\debug目錄,如下圖所示:
選擇【Debug屬性頁】->【鏈接器】->【輸入】->【附加依賴項(xiàng)】中,加入D:\env_build\libcurl_vs2013_x86\lib\x86\debug目錄下的libcurl_debug.lib文件名,如下圖所示:
當(dāng)然我使用的debug x86模式的程序,release的x86也類似,x64的也是,加上對應(yīng)的頭文件和庫文件即可。
頭文件加入后,相關(guān)的紅色錯(cuò)誤消失了,如下圖所示:
3、運(yùn)行l(wèi)ibcurlDemo1程序
添加好libcurl庫的頭文件和動(dòng)態(tài)庫后,運(yùn)行程序,發(fā)現(xiàn)出現(xiàn)如下的錯(cuò)誤:
說是找不到libcurl_debug.dll動(dòng)態(tài)庫文件,我們可以將剛才編譯生成D:\env_build\libcurl_vs2013_x86\bin目錄下的libcurl_debug.dll文件拷貝到libcurlDemo1項(xiàng)目生成的可執(zhí)行程序libcurlDemo1.exe所在目錄,入下圖所示:
然后再次運(yùn)行l(wèi)ibcurlDemo1程序,可以看到控制臺(tái)中輸出了從www.baidu.com頁面獲取的html頁面,如下圖所示:
4、使用屬性表文件
有時(shí)候?yàn)榱吮苊鉃槊總€(gè)項(xiàng)目都添加頭文件和lib依賴庫文件,可以使用為某個(gè)第三方依賴庫比如libcurl創(chuàng)建對應(yīng)的屬性表文件,以props為后綴,
在剛才的VS2013的libcurlDemo1項(xiàng)目中,針對debug的x86項(xiàng)目,創(chuàng)建一個(gè)libcurl_debug_x86_vs2013.props的屬性表文件,選擇【屬性頁管理器】->【Debug|Win32】,右鍵單擊,【添加新項(xiàng)目屬性表】,在【添加新項(xiàng)】對話框中輸入屬性表名稱,如下圖所示:
右鍵單擊libcurl_debug_x86_vs2013.props的屬性表文件,選擇【屬性】菜單,在libcurl_debug_x86_vs2013.props屬性頁的對話框中添加libcurl的頭文件和lib庫文件,同上。
5、相關(guān)資料
(1)build-libcurl-windows
github上面有一個(gè)項(xiàng)目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上面有一個(gè)項(xiàng)目build-libcurl-ios,是針對ios環(huán)境下的libcurl的編譯。