Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to statically link VCPKG produced .lib file in Visual Studio

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?

like image 480
Alex1844 Avatar asked Oct 22 '19 14:10

Alex1844


People also ask

What is static library in Visual Studio?

The sample projects in this article were created using Visual Studio 2010. A static library consists of object files that are linked together with an exe file. Object files are the output of compilers of unmanaged code and consists of functions that can only be used by unmanaged code.

How do I use CMake with Vcpkg?

CMake (Toolchain File) The best way to use installed libraries with cmake is via the toolchain file scripts\buildsystems\vcpkg. cmake . To use this file, you simply need to add it onto your CMake command line as: -DCMAKE_TOOLCHAIN_FILE=D:\src\vcpkg\scripts\buildsystems\vcpkg.


1 Answers

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

like image 197
Alex1844 Avatar answered Sep 29 '22 11:09

Alex1844