天天看點

vc2008建構和使用libcurl靜态庫

 2>用VC2008/2005打開工程curl-7.26.0\lib\libcurl.vcproj,轉換下工程并建構,可以直接編譯成功!

3>建立個控制台工程測試下剛才編譯的靜态庫libcurl.lib,可以在libcurl\curl-7.26.0\docs\examples目錄找個簡單的使用curl的例子,在這個工程選項Configuration Properties-| C/C++ -|General -|Additional Include Directories 路徑中加入curl7.26\include, 在linker頁籤,指定靜态庫路徑和靜态庫的名字libcurl.lib,代碼如下

#include "stdafx.h"  

#include <Windows.h>  

#include "curl/curl.h"  

int _tmain(int argc, _TCHAR* argv[])  

{  

    CURL *curl;  

    CURLcode res;  

    curl = curl_easy_init();  

    if(curl) {  

        curl_easy_setopt(curl, CURLOPT_URL, "http://2345.com/?kduba");  

        res = curl_easy_perform(curl);  

        curl_easy_cleanup(curl);  

    }  

    return 0;  

}  

 此時cpp檔案可以編譯,但是連結報錯

1>testcurl.obj : error LNK2001: unresolved external symbol __imp__curl_easy_init

1>testcurl.obj : error LNK2001: unresolved external symbol __imp__curl_easy_setopt

1>testcurl.obj : error LNK2001: unresolved external symbol __imp__curl_easy_perform

1>testcurl.obj : error LNK2001: unresolved external symbol __imp__curl_easy_cleanup

看樣子根本沒有連結靜态庫,雖然剛才指定了庫的路徑,确認庫路徑的名字沒錯,于是看了下curl_easy_init 這個函數的定義,

CURL_EXTERN CURL *curl_easy_init(void);  

CURL_EXTERN CURLcode curl_easy_setopt(CURL *curl, CURLoption option, ...);  

CURL_EXTERN CURLcode curl_easy_perform(CURL *curl);  

CURL_EXTERN void curl_easy_cleanup(CURL *curl);  

/* 

 * Decorate exportable functions for Win32 and Symbian OS DLL linking. 

 * This avoids using a .def file for building libcurl.dll. 

 */  

#if (defined(WIN32) || defined(_WIN32) || defined(__SYMBIAN32__)) && \  

     !defined(CURL_STATICLIB)  

#if defined(BUILDING_LIBCURL)  

#define CURL_EXTERN  __declspec(dllexport)  

#else  

#define CURL_EXTERN  __declspec(dllimport)  

#endif  

 看到這裡于是明白了,如下操作:

在libcurl靜态庫工程選項Configuration Properties-| C/C++ -| Preprocessor 中加上BUILDING_LIBCURL宏

在測試工程選項Configuration Properties-| C/C++ -| Preprocessor 中加上CURL_STATICLIB宏,然後依次重新建構兩個工程

發現測試工程連結不過

1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_unbind_s

1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_msgfree

1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ber_free

1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_memfree

1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_value_free_len

1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_get_values_len

1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_next_attribute

1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_first_attribute

1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_get_dn

1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_next_entry

1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_first_entry

1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_search_s

1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_simple_bind_s

1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_init

1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_set_option

1>libcurl_MT.lib(ldap.obj) : error LNK2001: unresolved external symbol __imp__ldap_err2string

谷歌了下, WSACleanup function msdn  是需要連結Ws2_32.lib,

同樣的道理

是少了Wldap32.lib

在libcurl靜态庫工程選項Configuration Properties-|Librarian -| Additional Dependencies 中加上依賴項Ws2_32.lib Wldap32.lib

再依次重編兩個工程,就OK了

編譯選項設為/MD時候,不需要添加Ws2_32.lib Wldap32.lib 

小結:

1>對于開源代碼的編譯問題,還是要從代碼入手,包括注釋

 2>靜态庫建構的時候很容易,但是要知道是不是成功的,還得編個測試工程才能知道是不是真的OK

繼續閱讀