在Visual Studio2013中編譯使用libcurl庫

關(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)如下圖所示:

libcurl庫的源代碼目錄結(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】并打開,如下圖所示:


Visual Studio Tools

我在自己的Windows10系統(tǒng)下同時(shí)安裝了Visual Studio 2017和Visual Studio 2013。
打開Visual Studio Tools,可以看到對應(yīng)的快捷鍵,如下圖所示:


Visual Studio Tools命令快捷鍵

為了使用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文件夾,用于存放編譯生成的文件。
如下圖所示:


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

而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
libcurl_vs2013_x86

為了方便,我一般把一些第三方庫放在系統(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,并選好程序存放的路徑,我的存放路徑為,如下圖所示:


使用VS2013新建一個(gè)Win32控制臺(tái)的項(xiàng)目

12

13

至此在Visual Studio 2013中生成了一個(gè)VC++控制臺(tái)的空項(xiàng)目,如下圖所示:


14

(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;
}
15

從上圖可以看到在沒有導(dǎo)入libcurl庫的頭文件時(shí)VS2013會(huì)提示紅色的錯(cuò)誤。
下面在VS2013中加入我們剛才編譯好的libcurl庫
在VS2013左下角的【屬性管理器】->【Debug|Win32】,右鍵單擊【Debug|Win32】,選擇【屬性】,進(jìn)入到【Debug屬性頁對話框】,如下圖所示:


16

17
  • 添加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目錄,如下圖所示:
19

20

選擇【Debug屬性頁】->【鏈接器】->【輸入】->【附加依賴項(xiàng)】中,加入D:\env_build\libcurl_vs2013_x86\lib\x86\debug目錄下的libcurl_debug.lib文件名,如下圖所示:


21

當(dāng)然我使用的debug x86模式的程序,release的x86也類似,x64的也是,加上對應(yīng)的頭文件和庫文件即可。

頭文件加入后,相關(guān)的紅色錯(cuò)誤消失了,如下圖所示:


22

3、運(yùn)行l(wèi)ibcurlDemo1程序

添加好libcurl庫的頭文件和動(dòng)態(tài)庫后,運(yùn)行程序,發(fā)現(xiàn)出現(xiàn)如下的錯(cuò)誤:

23

說是找不到libcurl_debug.dll動(dòng)態(tài)庫文件,我們可以將剛才編譯生成D:\env_build\libcurl_vs2013_x86\bin目錄下的libcurl_debug.dll文件拷貝到libcurlDemo1項(xiàng)目生成的可執(zhí)行程序libcurlDemo1.exe所在目錄,入下圖所示:
24

26

然后再次運(yùn)行l(wèi)ibcurlDemo1程序,可以看到控制臺(tái)中輸出了從www.baidu.com頁面獲取的html頁面,如下圖所示:
26

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)】對話框中輸入屬性表名稱,如下圖所示:


27

右鍵單擊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)境,本人親測好使。

28

(2)、build-libcurl-ios

github上面有一個(gè)項(xiàng)目build-libcurl-ios,是針對ios環(huán)境下的libcurl的編譯。

(3)、C++ 用libcurl庫進(jìn)行http通訊網(wǎng)絡(luò)編程

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,527評論 6 544
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,687評論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,640評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,957評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,682評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,011評論 1 329
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,009評論 3 449
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,183評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,714評論 1 336
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,435評論 3 359
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,665評論 1 374
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,148評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,838評論 3 350
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,251評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,588評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 52,379評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,627評論 2 380

推薦閱讀更多精彩內(nèi)容