簡體   English   中英

如何將 libcurl 構建為 static 庫並在項目中使用?

[英]How to build libcurl as static library and use in project?

我無法將 libcurl 編譯為 c++ 項目中的 static 庫。 構建包含 curl 的項目會導致 linker 錯誤。

我從https://curl.haxx.se/download.html下載了 curl 的源代碼。 我嘗試按照 curl-7.65.1\winbuild\BUILD.WINDOWS.txt 中的說明將其構建為 static 庫

  1. 運行 C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars32.bat (*vcvars32.bat)
  2. 在 VS17 cd..\curl-7.65.1\winbuild 的開發人員命令提示中
  3. 運行 nmake /f Makefile.vc mode=static VC=12 MACHINE=x86 DEBUG=no (*MACHINE=x64)
  4. 將curl-7.65.1\builds文件夾復制到vs17項目的項目文件夾中
  5. 添加 $(projectdir)builds\libcurl-vc12-x86-release-static-ipv6-sspi-winssl\include 到 Project/Properties/C++/General/Additional Include Directories (*x64)
  6. 添加 $(projectdir)builds\libcurl-vc12-x86-release-static-ipv6-sspi-winssl/lib 到 Project/Properties/Linker/General/Additional Library 目錄 (*x64)
  7. 將 libcurl_a.lib 添加到 Project/Properties/Linker/Input/Additional Dependencies
  8. 將 $(projectdir)builds 中的所有文件包含到項目中

我的 Visual Studio 編譯設置是 Release 和 x86

(我還嘗試了 x64 的整個過程,並在上述工作流程中修改 [標有 *]。結果是相同類型的 Linker 錯誤)

使用以下示例代碼測試 curl:

    #include <stdio.h>
    #include <iostream>

    #define CURL_STATICLIB
    #include "curl/curl.h"

    int main(void)
    {
        CURL *curl;
        CURLcode res;

        curl = curl_easy_init();
        if (curl) {
            curl_easy_setopt(curl, CURLOPT_URL, "https://google.com");
            /* example.com is redirected, so we tell libcurl to follow redirection */
            curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

            /* Perform the request, res will get the return code */
            res = curl_easy_perform(curl);
            /* Check for errors */
            if (res != CURLE_OK)
                fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    curl_easy_strerror(res));

            /* always cleanup */
            curl_easy_cleanup(curl);
        }

        std::cin.get();
    }
1 > ------Build started : Project: demoCurl, Configuration : Release Win32------
1 > Application.cpp
1 > nonblock.obj : error LNK2001 : unresolved external symbol __imp__ioctlsocket@12
1 > nonblock.obj : error LNK2001 : unresolved external symbol __imp__ioctlsocket@12
1 > sendf.obj : error LNK2001 : unresolved external symbol __imp__WSAGetLastError@0
1 > telnet.obj : error LNK2001 : unresolved external symbol __imp__WSAGetLastError@0
1 > tftp.obj : error LNK2001 : unresolved external symbol __imp__WSAGetLastError@0
1 > schannel.obj : error LNK2001 : unresolved external symbol __imp__WSAGetLastError@0
1 > asyn - thread.obj : error LNK2001 : unresolved external symbol __imp__WSAGetLastError@0
1 > connect.obj : error LNK2001 : unresolved external symbol __imp__WSAGetLastError@0
1 > ftp.obj : error LNK2001 : unresolved external symbol __imp__WSAGetLastError@0
1 > select.obj : error LNK2001 : unresolved external symbol __imp__WSAGetLastError@0
1 > connect.obj : error LNK2001 : unresolved external symbol __imp__bind@12
1 > ftp.obj : error LNK2001 : unresolved external symbol __imp__bind@12
1 > tftp.obj : error LNK2001 : unresolved external symbol __imp__bind@12
1 > connect.obj : error LNK2001 : unresolved external symbol __imp__closesocket@4
1 > connect.obj : error LNK2001 : unresolved external symbol __imp__connect@12
1 > connect.obj : error LNK2001 : unresolved external symbol __imp__getpeername@12
1 > connect.obj : error LNK2001 : unresolved external symbol __imp__getsockname@12
1 > ftp.obj : error LNK2001 : unresolved external symbol __imp__getsockname@12
1 > connect.obj : error LNK2001 : unresolved external symbol __imp__getsockopt@20
1 > smb.obj : error LNK2001 : unresolved external symbol __imp__htons@4
1 > socks_sspi.obj : error LNK2001 : unresolved external symbol __imp__htons@4
1 > telnet.obj : error LNK2001 : unresolved external symbol __imp__htons@4
1 > connect.obj : error LNK2001 : unresolved external symbol __imp__htons@4
1 > curl_addrinfo.obj : error LNK2001 : unresolved external symbol __imp__htons@4
1 > doh.obj : error LNK2001 : unresolved external symbol __imp__htons@4
1 > ftp.obj : error LNK2001 : unresolved external symbol __imp__htons@4
1 > connect.obj : error LNK2001 : unresolved external symbol __imp__ntohs@4
1 > ftp.obj : error LNK2001 : unresolved external symbol __imp__ntohs@4
1 > socks_sspi.obj : error LNK2001 : unresolved external symbol __imp__ntohs@4
1 > connect.obj : error LNK2001 : unresolved external symbol __imp__recv@16
1 > sendf.obj : error LNK2001 : unresolved external symbol __imp__recv@16
1 > connect.obj : error LNK2001 : unresolved external symbol __imp__setsockopt@20
1 > transfer.obj : error LNK2001 : unresolved external symbol __imp__setsockopt@20
1 > connect.obj : error LNK2001 : unresolved external symbol __imp__socket@12
1 > hostip6.obj : error LNK2001 : unresolved external symbol __imp__socket@12
1 > connect.obj : error LNK2001 : unresolved external symbol __imp__WSASetLastError@4
1 > curl_addrinfo.obj : error LNK2001 : unresolved external symbol __imp__WSASetLastError@4
1 > select.obj : error LNK2001 : unresolved external symbol __imp__WSASetLastError@4
1 > connect.obj : error LNK2001 : unresolved external symbol __imp__WSAIoctl@36
1 > transfer.obj : error LNK2001 : unresolved external symbol __imp__WSAIoctl@36
1 > curl_addrinfo.obj : error LNK2001 : unresolved external symbol __imp__getaddrinfo@16
1 > curl_addrinfo.obj : error LNK2001 : unresolved external symbol __imp__freeaddrinfo@4
1 > curl_gethostname.obj : error LNK2001 : unresolved external symbol __imp__gethostname@8
1 > curl_ntlm_core.obj : error LNK2001 : unresolved external symbol __imp__CryptAcquireContextA@20
1 > md5.obj : error LNK2001 : unresolved external symbol __imp__CryptAcquireContextA@20
1 > schannel.obj : error LNK2001 : unresolved external symbol __imp__CryptAcquireContextA@20
1 > curl_ntlm_core.obj : error LNK2001 : unresolved external symbol __imp__CryptReleaseContext@8
1 > md5.obj : error LNK2001 : unresolved external symbol __imp__CryptReleaseContext@8
1 > schannel.obj : error LNK2001 : unresolved external symbol __imp__CryptReleaseContext@8
1 > curl_ntlm_core.obj : error LNK2001 : unresolved external symbol __imp__CryptDestroyKey@4
1 > curl_ntlm_core.obj : error LNK2001 : unresolved external symbol __imp__CryptGetHashParam@20
1 > md5.obj : error LNK2001 : unresolved external symbol __imp__CryptGetHashParam@20
1 > schannel.obj : error LNK2001 : unresolved external symbol __imp__CryptGetHashParam@20
1 > curl_ntlm_core.obj : error LNK2001 : unresolved external symbol __imp__CryptImportKey@24
1 > curl_ntlm_core.obj : error LNK2001 : unresolved external symbol __imp__CryptEncrypt@28
1 > curl_ntlm_core.obj : error LNK2001 : unresolved external symbol __imp__CryptCreateHash@20
1 > md5.obj : error LNK2001 : unresolved external symbol __imp__CryptCreateHash@20
1 > schannel.obj : error LNK2001 : unresolved external symbol __imp__CryptCreateHash@20
1 > curl_ntlm_core.obj : error LNK2001 : unresolved external symbol __imp__CryptHashData@16
1 > md5.obj : error LNK2001 : unresolved external symbol __imp__CryptHashData@16
1 > schannel.obj : error LNK2001 : unresolved external symbol __imp__CryptHashData@16
1 > curl_ntlm_core.obj : error LNK2001 : unresolved external symbol __imp__CryptDestroyHash@4
1 > md5.obj : error LNK2001 : unresolved external symbol __imp__CryptDestroyHash@4
1 > schannel.obj : error LNK2001 : unresolved external symbol __imp__CryptDestroyHash@4
1 > ftp.obj : error LNK2001 : unresolved external symbol __imp__accept@12
1 > ftp.obj : error LNK2001 : unresolved external symbol __imp__listen@8
1 > idn_win32.obj : error LNK2001 : unresolved external symbol __imp__IdnToAscii@20
1 > idn_win32.obj : error LNK2001 : unresolved external symbol __imp__IdnToUnicode@20
1 > ldap.obj : error LNK2001 : unresolved external symbol __imp__ldap_init
1 > ldap.obj : error LNK2001 : unresolved external symbol __imp__ldap_sslinit
1 > ldap.obj : error LNK2001 : unresolved external symbol __imp__ldap_unbind_s
1 > ldap.obj : error LNK2001 : unresolved external symbol __imp__ldap_set_option
1 > ldap.obj : error LNK2001 : unresolved external symbol __imp__ldap_simple_bind_s
1 > ldap.obj : error LNK2001 : unresolved external symbol __imp__ldap_bind_s
1 > ldap.obj : error LNK2001 : unresolved external symbol __imp__ldap_search_s
1 > ldap.obj : error LNK2001 : unresolved external symbol __imp__ldap_msgfree
1 > ldap.obj : error LNK2001 : unresolved external symbol __imp__ldap_err2string
1 > ldap.obj : error LNK2001 : unresolved external symbol __imp__ldap_first_entry
1 > ldap.obj : error LNK2001 : unresolved external symbol __imp__ldap_next_entry
1 > ldap.obj : error LNK2001 : unresolved external symbol __imp__ldap_first_attribute
1 > ldap.obj : error LNK2001 : unresolved external symbol __imp__ldap_next_attribute
1 > ldap.obj : error LNK2001 : unresolved external symbol __imp__ldap_get_values_len
1 > ldap.obj : error LNK2001 : unresolved external symbol __imp__ldap_value_free_len
1 > ldap.obj : error LNK2001 : unresolved external symbol __imp__ldap_get_dn
1 > ldap.obj : error LNK2001 : unresolved external symbol __imp__ldap_memfree
1 > ldap.obj : error LNK2001 : unresolved external symbol __imp__ber_free
1 > select.obj : error LNK2001 : unresolved external symbol ___WSAFDIsSet@8
1 > select.obj : error LNK2001 : unresolved external symbol __imp__select@20
1 > sendf.obj : error LNK2001 : unresolved external symbol __imp__send@16
1 > telnet.obj : error LNK2001 : unresolved external symbol __imp__send@16
1 > system_win32.obj : error LNK2001 : unresolved external symbol __imp__WSAStartup@8
1 > telnet.obj : error LNK2001 : unresolved external symbol __imp__WSAStartup@8
1 > system_win32.obj : error LNK2001 : unresolved external symbol __imp__WSACleanup@0
1 > telnet.obj : error LNK2001 : unresolved external symbol __imp__WSACleanup@0
1 > tftp.obj : error LNK2001 : unresolved external symbol __imp__recvfrom@24
1 > tftp.obj : error LNK2001 : unresolved external symbol __imp__sendto@24
1 > krb5_sspi.obj : error LNK2001 : unresolved external symbol __imp__htonl@4
1 > krb5_sspi.obj : error LNK2001 : unresolved external symbol __imp__ntohl@4
1 > schannel.obj : error LNK2001 : unresolved external symbol __imp__CryptGenRandom@12
1 > schannel.obj : error LNK2001 : unresolved external symbol __imp__CertOpenStore@20
1 > schannel_verify.obj : error LNK2001 : unresolved external symbol __imp__CertOpenStore@20
1 > schannel.obj : error LNK2001 : unresolved external symbol __imp__CertCloseStore@8
1 > schannel_verify.obj : error LNK2001 : unresolved external symbol __imp__CertCloseStore@8
1 > schannel.obj : error LNK2001 : unresolved external symbol __imp__CertEnumCertificatesInStore@8
1 > schannel.obj : error LNK2001 : unresolved external symbol __imp__CertFindCertificateInStore@24
1 > schannel.obj : error LNK2001 : unresolved external symbol __imp__CertFreeCertificateContext@4
1 > schannel_verify.obj : error LNK2001 : unresolved external symbol __imp__CertFreeCertificateContext@4
1 > schannel.obj : error LNK2001 : unresolved external symbol __imp__CryptStringToBinaryA@28
1 > schannel_verify.obj : error LNK2001 : unresolved external symbol __imp__CertAddCertificateContextToStore@16
1 > schannel_verify.obj : error LNK2001 : unresolved external symbol __imp__CertGetNameStringA@24
1 > schannel_verify.obj : error LNK2001 : unresolved external symbol __imp__CryptQueryObject@44
1 > schannel_verify.obj : error LNK2001 : unresolved external symbol __imp__CertCreateCertificateChainEngine@8
1 > schannel_verify.obj : error LNK2001 : unresolved external symbol __imp__CertFreeCertificateChainEngine@4
1 > schannel_verify.obj : error LNK2001 : unresolved external symbol __imp__CertGetCertificateChain@32
1 > schannel_verify.obj : error LNK2001 : unresolved external symbol __imp__CertFreeCertificateChain@4
1 > C:\...\demoCurl\Release\demoCurl.exe : fatal error LNK1120 : 72 unresolved externals
1 > Done building project "demoCurl.vcxproj" --FAILED.
========== Build: 0 succeeded, 1 failed, 0 up - to - date, 0 skipped ==========

我使用以下命令構建

nmake /f Makefile.vc mode=static RTLIBCFG=static DEBUG=yes ENABLE_IDN=no ENABLE_SSPI=no

並鏈接到

libcurl_a_debug.lib ws2_32.lib wldap32.lib

下面的答案對我有用。 但是如果你不知道nmakeMakefile.vc ,你需要關注這個,請看這個線程。 https://stackoverflow.com/a/54680718/19178910

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM