我正在尝试将 libcurl 库静态链接到我的可执行文件中,这样我就可以在其他计算机上运行我的可执行文件,而无需 dll 文件,因为所有必要的文件都烘焙在我的可执行文件中
我已经从 vcpkg 安装了curl
vcpkg install curl
在 Visual Studio 中配置我的 C++ 项目以静态链接 libcurl:
配置属性:[发布]
C:\dev\vcpkg\installed\x64-windows\include
CURL_STATICLIB
Multi-threaded (/MT)
C:\dev\vcpkg\installed\x64-windows\lib
libcrypto.lib; libssl.lib; libcurl.lib
在发布模式下构建解决方案,Visual Studio 编译它没有任何问题。可执行文件在我的机器上运行正常。
但不运行另一台机器:
The code execution cannot proceed because libcurl.dll was not found.
Reinstalling the program may fix this problem.
我想知道我做错了什么?也许curl不能静态组合? 如果我的配置有问题,那么为什么我包含的其他库不会引发错误?
此外,您还推荐哪些其他库用于在 C++ 中发出 http 请求?我想要易于使用的,可以静态编译成可执行文件而没有任何问题的。
阅读本文评论中发布的@DeanVanGreunen的指南,我意识到我可以从
x64-windows-static
安装vcpkg
版本的软件包。
因此,我安装了 curl:x64-windows-static
,它将“静态”版本的curl 安装到 vcpkg 目录内单独的 x64-windows-static
文件夹中。
我将项目解决方案配置为链接位于文件夹中的
libcurl.lib
。我尝试了手动(调整配置属性)和自动(通过 vcpkg integrate install
命令引导并根据指南更改 .vcxproj 文件)链接解决方案。
两者都不起作用,现在我什至无法构建应用程序!
这是构建输出日志:https://pastebin.com/JVx4Z2P4
感谢 @Alan Birtles 在评论中留下了 github/vcpkg 问题页面的链接,解决了问题。我将在这里为将来面临同样问题的人留下该问题的逐步解决方案。
x64-windows-static
安装
vcpkg
版本的软件包:vcpkg install curl:x64-windows-static
include
和
library
目录路径:
来自 ...\vcpkg\installed\x64-windows\...
...\vcpkg\installed\x64-windows-static\...
编辑
.vcxproj
文件即可(感谢 @Dean Van Greunen 的建议):
Find `<PropertyGroup Label=”Globals”>` tag and include this inside:
```xml
<VcpkgTriplet Condition="'$(Platform)'=='x64'">x64-windows-static</VcpkgTriplet>
```
And you are ready to go!
#pragma comment(lib, "Ws2_32.Lib")
#pragma comment(lib, "Wldap32.Lib")
#pragma comment(lib, "Crypt32.Lib")