在Visual Studio2013中編譯使用libcurl庫

關于libcurl庫

libcurl是一個跨平臺的網絡協(xié)議庫,支持http, https, ftp, gopher, telnet, dict, file, 和ldap 協(xié)議。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。下載后解壓到某個目錄下,對應的文件目錄結構如下圖所示:

libcurl庫的源代碼目錄結構

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】并打開,如下圖所示:


Visual Studio Tools

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


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


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庫??梢愿鶕枰x擇自己所需的版本。
這里我選擇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
libcurl_vs2013_x86

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


使用VS2013新建一個Win32控制臺的項目

12

13

至此在Visual Studio 2013中生成了一個VC++控制臺的空項目,如下圖所示:


14

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

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


16

17
  • 添加libcurl的頭文件所在目錄
    選擇【C/C++】->【常規(guī)】->【附加包含目錄】,在【附加包含目錄】中添加libcurl所在的頭文件路徑,我的是:D:\env_build\libcurl_vs2013_x86\include
    如下圖所示:


    18
  • 添加libcurl的debug動態(tài)庫目錄
    依次選擇【Debug屬性頁】->【鏈接器】->【常規(guī)】,在附加庫目錄中添加剛才編譯好的lib庫所在目錄,如D:\env_build\libcurl_vs2013_x86\lib\x86\debug目錄,如下圖所示:
19

20

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


21

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

頭文件加入后,相關的紅色錯誤消失了,如下圖所示:


22

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

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

23

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

26

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

4、使用屬性表文件

有時候為了避免為每個項目都添加頭文件和lib依賴庫文件,可以使用為某個第三方依賴庫比如libcurl創(chuàng)建對應的屬性表文件,以props為后綴,
在剛才的VS2013的libcurlDemo1項目中,針對debug的x86項目,創(chuàng)建一個libcurl_debug_x86_vs2013.props的屬性表文件,選擇【屬性頁管理器】->【Debug|Win32】,右鍵單擊,【添加新項目屬性表】,在【添加新項】對話框中輸入屬性表名稱,如下圖所示:


27

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

28

(2)、build-libcurl-ios

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

(3)、C++ 用libcurl庫進行http通訊網絡編程

?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容