<menu id="w8yyk"><menu id="w8yyk"></menu></menu>
  • <dd id="w8yyk"><nav id="w8yyk"></nav></dd>
    <menu id="w8yyk"></menu>
    <menu id="w8yyk"><code id="w8yyk"></code></menu>
    <menu id="w8yyk"></menu>
    <xmp id="w8yyk">
    <xmp id="w8yyk"><nav id="w8yyk"></nav>
  • 網站首頁 > 物聯資訊 > 技術分享

    vc2008構建和使用libcurl靜態庫

    2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀
    睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接

    1>下載CURL源代碼curl-7.26.0.zip

     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,代碼如下

     

    [cpp] view plaincopy  
    1. #include "stdafx.h"  
    2. #include <Windows.h>  
    3. #include "curl/curl.h"  
    4.   
    5. int _tmain(int argc, _TCHAR* argv[])  
    6. {  
    7.     CURL *curl;  
    8.     CURLcode res;  
    9.   
    10.     curl = curl_easy_init();  
    11.     if(curl) {  
    12.         curl_easy_setopt(curl, CURLOPT_URL, "http://2345.com/?kduba");  
    13.         res = curl_easy_perform(curl);  
    14.         curl_easy_cleanup(curl);  
    15.     }  
    16.     return 0;  
    17. }  

     

     此時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 這個函數的定義,

     

    [cpp] view plaincopy  
    1. CURL_EXTERN CURL *curl_easy_init(void);  
    2. CURL_EXTERN CURLcode curl_easy_setopt(CURL *curl, CURLoption option, ...);  
    3. CURL_EXTERN CURLcode curl_easy_perform(CURL *curl);  
    4. CURL_EXTERN void curl_easy_cleanup(CURL *curl);  
    5.   
    6.   
    7. /* 
    8.  * Decorate exportable functions for Win32 and Symbian OS DLL linking. 
    9.  * This avoids using a .def file for building libcurl.dll. 
    10.  */  
    11. #if (defined(WIN32) || defined(_WIN32) || defined(__SYMBIAN32__)) && \  
    12.      !defined(CURL_STATICLIB)  
    13. #if defined(BUILDING_LIBCURL)  
    14. #define CURL_EXTERN  __declspec(dllexport)  
    15. #else  
    16. #define CURL_EXTERN  __declspec(dllimport)  
    17. #endif  
    18. #else  

     

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

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

    發現測試工程鏈接不過

    1>libcurl_MT.lib(easy.obj) : error LNK2001: unresolved external symbol __imp__WSACleanup@0
    1>libcurl_MT.lib(telnet.obj) : error LNK2001: unresolved external symbol __imp__WSACleanup@0
    1>libcurl_MT.lib(easy.obj) : error LNK2001: unresolved external symbol __imp__WSAStartup@8
    1>libcurl_MT.lib(telnet.obj) : error LNK2001: unresolved external symbol __imp__WSAStartup@8
    1>libcurl_MT.lib(tftp.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
    1>libcurl_MT.lib(telnet.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
    1>libcurl_MT.lib(ftp.obj) : error LNK2001: unresolved external symbol__imp__WSAGetLastError@0
    1>libcurl_MT.lib(select.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
    1>libcurl_MT.lib(asyn-thread.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
    1>libcurl_MT.lib(transfer.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
    1>libcurl_MT.lib(sendf.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
    1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
    1>libcurl_MT.lib(sendf.obj) : error LNK2001: unresolved external symbol __imp__send@16
    1>libcurl_MT.lib(telnet.obj) : error LNK2001: unresolved external symbol __imp__send@16
    1>libcurl_MT.lib(sendf.obj) : error LNK2001: unresolved external symbol __imp__recv@16
    1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__recv@16
    1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__getsockname@12
    1>libcurl_MT.lib(ftp.obj) : error LNK2001: unresolved external symbol__imp__getsockname@12
    1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__getpeername@12
    1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__ntohs@4
    1>libcurl_MT.lib(telnet.obj) : error LNK2001: unresolved external symbol __imp__ntohs@4
    1>libcurl_MT.lib(ftp.obj) : error LNK2001: unresolved external symbol__imp__ntohs@4
    1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__WSASetLastError@4
    1>libcurl_MT.lib(curl_addrinfo.obj) : error LNK2001: unresolved external symbol __imp__WSASetLastError@4
    1>libcurl_MT.lib(select.obj) : error LNK2001: unresolved external symbol __imp__WSASetLastError@4
    1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__getsockopt@20
    1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__setsockopt@20
    1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__connect@12
    1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__bind@12
    1>libcurl_MT.lib(tftp.obj) : error LNK2001: unresolved external symbol __imp__bind@12
    1>libcurl_MT.lib(ftp.obj) : error LNK2001: unresolved external symbol__imp__bind@12
    1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__htons@4
    1>libcurl_MT.lib(curl_addrinfo.obj) : error LNK2001: unresolved external symbol __imp__htons@4
    1>libcurl_MT.lib(telnet.obj) : error LNK2001: unresolved external symbol __imp__htons@4
    1>libcurl_MT.lib(ftp.obj) : error LNK2001: unresolved external symbol__imp__htons@4
    1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__closesocket@4
    1>libcurl_MT.lib(connect.obj) : error LNK2001: unresolved external symbol __imp__socket@12
    1>libcurl_MT.lib(curl_addrinfo.obj) : error LNK2001: unresolved external symbol __imp__freeaddrinfo@4
    1>libcurl_MT.lib(curl_addrinfo.obj) : error LNK2001: unresolved external symbol __imp__getaddrinfo@16
    1>libcurl_MT.lib(tftp.obj) : error LNK2001: unresolved external symbol __imp__sendto@24
    1>libcurl_MT.lib(tftp.obj) : error LNK2001: unresolved external symbol __imp__recvfrom@24
    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
    1>libcurl_MT.lib(ftp.obj) : error LNK2001: unresolved external symbol__imp__listen@8
    1>libcurl_MT.lib(ftp.obj) : error LNK2001: unresolved external symbol__imp__accept@12
    1>libcurl_MT.lib(select.obj) : error LNK2001: unresolved external symbol ___WSAFDIsSet@8
    1>libcurl_MT.lib(select.obj) : error LNK2001: unresolved external symbol __imp__select@20
    1>libcurl_MT.lib(nonblock.obj) : error LNK2001: unresolved external symbol __imp__ioctlsocket@12
    1>libcurl_MT.lib(curl_gethostname.obj) : error LNK2001: unresolved external symbol__imp__gethostname@8

    谷歌了下, WSACleanup function msdn  是需要鏈接Ws2_32.lib,

    同樣的道理

    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

    是少了Wldap32.lib

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

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

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

     

    小結:

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

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

    RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
    最近免费观看高清韩国日本大全