如何在Visual Studio中静态链接VCPKG生成的.lib文件 [英] How to statically link VCPKG produced .lib file in Visual Studio

查看:704
本文介绍了如何在Visual Studio中静态链接VCPKG生成的.lib文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我使用VCPKG构建像libcurl这样的第三方库.我有动态和静态版本.显然,import .lib和object .lib文件包含在两个不同的文件夹中,分别是x64-windows和x64-windows-static. 我想将对象libcurl.lib静态链接到我的程序,但无法弄清楚如何配置Visual Studio来执行此操作.它总是最终使用导入库而不是对象库,因此我的程序最终在执行时需要libcurl.dll.

我已经将主项目配置为使用/MT运行时库. 我尝试将链接器配置为使用指向静态lib文件夹和其他依赖项的其他库文件夹. 我什至尝试在链接器其他依赖项中使用对象libcurl.lib文件的完整路径.

当我们使用/MT切换运行时库时,我可以通过使用Dependency Walker得知所有常规库(如vcruntime)都集成在可执行文件中,而不是作为DLL加载,但是libucurl.dll仍然存在于其中,可以动态加载./p>

似乎由于VCPKG自动包含路径集成,即使我在链接器输入中指定了对象libcurl.lib的完整路径,Visual Studio也会始终首先查找并使用导入libcurl.lib.

如何配置Visual Studio以静态链接正确的.lib文件?

解决方案

好的,我找到了可能遇到相同问题的人的解决方案.

默认情况下,Visual Studio使用x64-windows或x86-windows"triplet"

通过将MSBuild的详细程度设置为普通"或更高级别,您可以看到自动推论的三元组:

快捷方式:Ctrl + Q构建并运行"

工具->选项->项目和解决方案->生成并运行-> MSBuild项目生成输出详细信息

要覆盖自动选择的三元组并使用x64-windows-static或x86-windows-static,可以在.vcxproj中指定MSBuild属性VcpkgTriplet,方法是将其添加到Globals PropertyGroup中.

<PropertyGroup Label="Globals">
  <!-- .... -->
  <VcpkgTriplet Condition="'$(Platform)'=='Win32'">x86-windows-static</VcpkgTriplet>
  <VcpkgTriplet Condition="'$(Platform)'=='x64'">x64-windows-static</VcpkgTriplet>
</PropertyGroup>

https://github.com/microsoft/vcpkg/blob/master/docs/users/integration.md#with-msbuild

I use VCPKG to build third party libraries like libcurl for example. I have dynamic and static builds. Obviously, import .lib and object .lib files are contained in two different folders, x64-windows and x64-windows-static respectively. I want to link object libcurl.lib statically with my program, but cannot figure out how to configure Visual Studio to do it. It always ends up using the import lib, rather than object lib and thus my program ends up requiring libcurl.dll at execution.

I have configured the main project to use /MT runtime library. I have tried configuring linker to use additional libraries folder pointing to the static lib folder and additional dependencies. I have even tried using full path to the object libcurl.lib file in linker additional dependencies.

When I us /MT switch for runtime libraries, I can tell by using Dependency Walker that all the regular libraries like vcruntime are integrated in the executable, not loaded as DLLs, but libucurl.dll is still there, loaded dynamically.

It seems that due to VCPKG automatic include path integration, Visual Studio always finds and uses the import libcurl.lib first, even if I specify the full path of the object libcurl.lib in linker inputs.

How do I configure Visual Studio to statically link the right .lib file?

解决方案

OK, I found the solution for anyone who might have the same problem.

By default, Visual Studio uses x64-windows, or x86-windows "triplet"

You can see the automatically deduced triplet by setting your MSBuild verbosity to Normal or higher:

Shortcut: Ctrl+Q "build and run"

Tools -> Options -> Projects and Solutions -> Build and Run -> MSBuild project build output verbosity

To override the automatically chosen triplet and use x64-windows-static or x86-windows-static, you can specify the MSBuild property VcpkgTriplet in your .vcxproj by adding this to the Globals PropertyGroup.

<PropertyGroup Label="Globals">
  <!-- .... -->
  <VcpkgTriplet Condition="'$(Platform)'=='Win32'">x86-windows-static</VcpkgTriplet>
  <VcpkgTriplet Condition="'$(Platform)'=='x64'">x64-windows-static</VcpkgTriplet>
</PropertyGroup>

https://github.com/microsoft/vcpkg/blob/master/docs/users/integration.md#with-msbuild

这篇关于如何在Visual Studio中静态链接VCPKG生成的.lib文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆