Windows下編譯並使用libcurl.lib靜態庫

來源:互聯網
上載者:User

1.下載最新版的libcurl,我的是curl-7.44.0.zip,解壓,進入目錄winbuild,仔細閱讀BUILD.WINDOWS.txt這篇文檔

開啟VS2013 開發人員命令提示字元並執行nmake /f Makefile.vc mode=static VC=12

完成編譯後libcurl靜態庫會出現在builds目錄下包括標頭檔和libcurl_a.lib,我們需要的是builds/libcurl-vc12-x86-release-static-ipv6-sspi-winssl下面的include和lib兩個檔案夾

文檔裡面一段這樣的話

Static linking of Microsoft's C RunTime (CRT):
==============================================
If you are using mode=static nmake will create and link to the static build of
libcurl but *not* the static CRT. If you must you can force nmake to link in
the static CRT by passing RTLIBCFG=static. Typically you shouldn't use that
option, and nmake will default to the DLL CRT. RTLIBCFG is rarely used and
therefore rarely tested. When passing RTLIBCFG for a configuration that was
already built but not with that option, or if the option was specified
differently, you must destroy the build directory containing the configuration
so that nmake can build it from scratch.

VS產生代碼的時候有一個運行庫選項(項目屬性 - 配置屬性 - C/C++ - 代碼產生 - 運行庫)/MT和/MD(/MTd和/MDd是對應的debug版本)

就是說你編譯時間如果加了RTLIBCFG=static這個選項,就相當於編譯了/MT版本的libcurl,否則是/MD版本的


2.開啟VS2013建立項目選擇Win32控制台應用程式,附加選項選擇空項目

然後把前面編譯出來的標頭檔夾和lib檔案夾複製到項目所在路徑。工程右鍵添加-現有項,把libcurl_a.lib加入工程

建立一個cpp檔案,測試一下能否使用

#include "include/curl/curl.h"int main(){curl_easy_init();return 0;}
報錯:

error LNK2019: 無法解析的外部符號 __imp__curl_easy_init,該符號在函數 _main 中被引用

fatal error LNK1120: 1 個無法解析的外部命令

libcurl沒有連結到程式中,找不到這個函數入口

解決方案:開啟項目屬性,配置屬性 - C/C++ - 前置處理器 - 前置處理器定義 - 開啟下拉框 - 編輯 - 文字框輸入CURL_STATICLIB - 儲存(或者在curl.h中#define CURL_STATICLIB)

重新編譯

報錯:

1>LIBCMT.lib(invarg.obj) : error LNK2005: __invoke_watson 已經在 MSVCRTD.lib(MSVCR120D.dll) 中定義
1>LIBCMT.lib(lseeki64.obj) : error LNK2005: __lseeki64 已經在 MSVCRTD.lib(MSVCR120D.dll) 中定義
1>LIBCMT.lib(dosmap.obj) : error LNK2005: __errno 已經在 MSVCRTD.lib(MSVCR120D.dll) 中定義
1>LIBCMT.lib(tolower.obj) : error LNK2005: _tolower 已經在 MSVCRTD.lib(MSVCR120D.dll) 中定義
1>LIBCMT.lib(wsetloca.obj) : error LNK2005: __configthreadlocale 已經在 MSVCRTD.lib(MSVCR120D.dll) 中定義
1>LIBCMT.lib(crt0dat.obj) : error LNK2005: __amsg_exit 已經在 MSVCRTD.lib(MSVCR120D.dll) 中定義
1>LIBCMT.lib(crt0dat.obj) : error LNK2005: __cexit 已經在 MSVCRTD.lib(MSVCR120D.dll) 中定義
1>LIBCMT.lib(crt0dat.obj) : error LNK2005: __exit 已經在 MSVCRTD.lib(MSVCR120D.dll) 中定義
1>LIBCMT.lib(crt0dat.obj) : error LNK2005: __initterm_e 已經在 MSVCRTD.lib(MSVCR120D.dll) 中定義
1>LIBCMT.lib(crt0dat.obj) : error LNK2005: _exit 已經在 MSVCRTD.lib(MSVCR120D.dll) 中定義
1>LIBCMT.lib(mlock.obj) : error LNK2005: __lock 已經在 MSVCRTD.lib(MSVCR120D.dll) 中定義
1>LIBCMT.lib(mlock.obj) : error LNK2005: __unlock 已經在 MSVCRTD.lib(MSVCR120D.dll) 中定義
1>LIBCMT.lib(winapisupp.obj) : error LNK2005: ___crtSetUnhandledExceptionFilter 已經在 MSVCRTD.lib(MSVCR120D.dll) 中定義
1>LIBCMT.lib(winapisupp.obj) : error LNK2005: ___crtTerminateProcess 已經在 MSVCRTD.lib(MSVCR120D.dll) 中定義
1>LIBCMT.lib(winapisupp.obj) : error LNK2005: ___crtUnhandledException 已經在 MSVCRTD.lib(MSVCR120D.dll) 中定義
1>LIBCMT.lib(_file.obj) : error LNK2005: ___iob_func 已經在 MSVCRTD.lib(MSVCR120D.dll) 中定義
1>LIBCMT.lib(winxfltr.obj) : error LNK2005: __XcptFilter 已經在 MSVCRTD.lib(MSVCR120D.dll) 中定義
1>LIBCMT.lib(hooks.obj) : error LNK2005: "void __cdecl terminate(void)" (?terminate@@YAXXZ) 已經在 MSVCRTD.lib(MSVCR120D.dll) 中定義
1>LIBCMT.lib(crt0init.obj) : error LNK2005: ___xi_a 已經在 MSVCRTD.lib(cinitexe.obj) 中定義
1>LIBCMT.lib(crt0init.obj) : error LNK2005: ___xi_z 已經在 MSVCRTD.lib(cinitexe.obj) 中定義
1>LIBCMT.lib(crt0init.obj) : error LNK2005: ___xc_a 已經在 MSVCRTD.lib(cinitexe.obj) 中定義
1>LIBCMT.lib(crt0init.obj) : error LNK2005: ___xc_z 已經在 MSVCRTD.lib(cinitexe.obj) 中定義
1>LIBCMT.lib(errmode.obj) : error LNK2005: ___set_app_type 已經在 MSVCRTD.lib(MSVCR120D.dll) 中定義
1>LIBCMT.lib(fflush.obj) : error LNK2005: _fflush 已經在 MSVCRTD.lib(MSVCR120D.dll) 中定義
1>LINK : warning LNK4098: 預設庫“MSVCRTD”與其他庫的使用衝突;請使用 /NODEFAULTLIB:library
1>LINK : warning LNK4098: 預設庫“LIBCMT”與其他庫的使用衝突;請使用 /NODEFAULTLIB:library
1>e:\documents\visual studio 2013\Projects\ConsoleApplication5\Debug\ConsoleApplication5.exe : fatal error LNK1169: 找到一個或多個多重定義的符號

解決方案:項目屬性 - 配置屬性 - C/C++ - 代碼產生 - 運行庫,根據編譯的libcurl版本選擇/MT或/MD,把項目設定成release,平台x86

重新編譯運行成功。


vc2008構建和使用libcurl靜態庫:http://blog.csdn.net/mos2046/article/details/7697530


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.