visual-studio - 在 Visual Studio 2019 中使用 libcurl 时生成错误
问题描述
我正在尝试让 libcurl 工作。我是第一次使用 libcurl。我使用 vcpkg 安装了 libcurl。
C:\Users\infib\vcpkg>vcpkg install curl
The following packages are already installed:
curl[core,ssl,tool,winssl]:x86-windows
Starting package 1/1: curl:x86-windows
Package curl:x86-windows is already installed
Elapsed time for package curl:x86-windows: 4.298 ms
Total elapsed time: 4.956 ms
The package curl:x86-windows provides CMake targets:
find_package(CURL CONFIG REQUIRED)
target_link_libraries(main PRIVATE CURL::libcurl)
我安装了 opencv64 位,我正在尝试使用 libcurl,但我遇到了很多错误。
error LNK2019: unresolved external symbol __imp_curl_easy_init
error LNK2019: unresolved external symbol __imp_curl_easy_setopt
error LNK2019: unresolved external symbol __imp_curl_easy_perform
error LNK2019: unresolved external symbol __imp_curl_easy_cleanup
error LNK2019: unresolved external symbol __imp_curl_easy_getinfo
1>libcurl_a.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
1>libcurl-d.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
1>libcurl.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
1>WINMM.LIB : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
1>wldap32.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
1>ws2_32.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
有人可以告诉我如何克服这个错误。我包含了在 vcpkg 包文件夹中找到的 libcurl.lib,但我仍然收到此错误。我尝试了stackover flow libcurl线程中提到的所有方法,但仍然出现错误。
请让我知道如何解决这个问题。
解决方案
看起来您问题的核心是 x64 与 x86。vcpkg 可能正在引入 x86 构建。老实说,我使用的解决方案是直接拉出 curl 的源代码并使用 cmake。获得源代码后,您需要运行这样的脚本(在 curl 目录中):
mkdir build32 pushd build32 cmake -G "Visual Studio 16 2019" -A Win32 .. popd cmake --build build32 // 这将为你编译它
mkdir build64 pushd build64 cmake -G "Visual Studio 16 2019" -A x64 .. popd cmake --build build64 //同上
这将创建一个共享库。如果你想要一个静态库,你需要进入 CMakeLists.txt 文件并更新
选项(BUILD_SHARED_LIBS“构建共享库”开启)
至
选项(BUILD_SHARED_LIBS“构建共享库”关闭)
这将启用静态构建。
您将在相应的构建库和类型文件夹下找到必要的 .lib 和 .dll 文件,例如 debug 64 将是 build64\lib\Debug
希望这可以帮助。