How to statically link VCPKG produced .lib file in Visual Studio - 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?

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

Related

How to commit a project to SVN which has absolute paths from cmake build? [duplicate]

I am able to CMake build this HelloWorld example project using cmakelists.txt file and generate a visual studio project.
project(helloworld LANGUAGES C CXX)
cmake_minimum_required(VERSION 3.5)
find_package(Idlpp-cxx REQUIRED)
if (NOT TARGET CycloneDDS-CXX::ddscxx)
find_package(CycloneDDS-CXX REQUIRED)
endif()
# Convenience function, provided by the Idlpp-cxx that generates a CMake
# target for the given IDL file. The function calls Idlcpp-cxx to generate
# source files and compiles them into a library.
idl_ddscxx_generate(ddscxxHelloWorldData_lib "HelloWorldData.idl")
add_executable(ddscxxHelloworldPublisher publisher.cpp)
add_executable(ddscxxHelloworldSubscriber subscriber.cpp)
# Link both executables to idl data type library and ddscxx.
target_link_libraries(ddscxxHelloworldPublisher ddscxxHelloWorldData_lib CycloneDDS-CXX::ddscxx)
target_link_libraries(ddscxxHelloworldSubscriber ddscxxHelloWorldData_lib CycloneDDS-CXX::ddscxx)
set_property(TARGET ddscxxHelloworldPublisher PROPERTY CXX_STANDARD 11)
set_property(TARGET ddscxxHelloworldSubscriber PROPERTY CXX_STANDARD 11)
I need to create the same project without cmakelists.txt and CMake
How to do this only using visual studio? where to define those commands in CMakelists.txt in visual studio if I create an empty c++ project
I have tried this making an empty project.
I don't know how to idl_ddscxx_generate and target_link_libraries perform in VS....
idl_ddscxx_generate has to run if IDL file has changed
target_link_libraries is required if I added new source files to the project....
Make a new, empty Visual Studio project.
Copy all source files except the CMake files.
Do whatever you do in a Visual Studio project. Add files, targets, dependecies, … If you are not sure, look up what is written in the CMakeLists.txt file.
Delete all CMake files in your original project and copy your Visual Studio project files.
Add these changes (deleted CMake files, added VS project files) to your Subversion repository, maybe do this in a branch that others can test it, report back, and improve the change. Once done, merge the branch.
Probably, add step 0.: Learn how Visual Studio organizes its project. Make a tutorial, take some training.
Remark: Whatever your problem is with CMake, you missed something. But you can find this out later and revert your changes and pick up CMake up again.

Including the Expat library in Visual Studio 2013

I've downloaded a copy of Expat 2.0.1, which is basically a bunch of .h and .c files (see the complete directory in the screenshot below).
To add the library to the VS 2013 project I'm working on, I went to Properties -> Configuration Properties -> VC++ Directories and added the path of the 2.0.1 folder shown in the screenshot to the "Include Directories" field. This gets rid of all the intellisense errors, but when I build the project I get a bunch of linker errors that are complaining about unresolved symbols (eg XML_ParserCreate, which is a function from the Expat library). I can right-click on these symbols and peek the definition, so it seems like everything is "hooked up" correctly. How can I get the linker to stop complaining? There doesn't seem to be any libraries I can add to the Linker section of the configuration. Do I need to use Makefile.MPW somehow?
This is just the source, you have to create the library yourself, see MicrosoftDocs. BTW MPW is a Mac file.

How to debug cmake target after install

I have a cross-platform project which uses cmake in order to generate Visual Studio solution files. The project has external dependencies (.dlls, resources etc) and the only place where the executable can be run is the installation directory. In that directory I have access all the resources, plugins, translations etc. I can install the project both in debug or release in that directory.
How can I debug a project in the installed location?
There are two problems with this case:
Sometimes I may debug the main application (Main.exe) (a target in cmake project)
Sometimes I may debug some plugins that Main.exe loads when started (I have a different cmake project for the plugins)
Is there a clean way of doing this in Visual C++ without actually create some custom project that is configured to start each time the Main.exe from the installed location? ("C:\Program Files\MainProject\Main.exe" )
Thank you,
Iulian
I managed to do it by 'configuring' with cmake a .user file for the specified project.
The only thing that the user needs to do is to use a template like in this bitbucket project.
In the project you can find a template file.
The cmake script command needed is:
CONFIGURE_FILE(
"${PROJECT_SOURCE_DIR}/scripts/windows/VS201x_Template.vcxproj.user.in"
"${PROJECT_BINARY_DIR}/INSTALL.vcxproj.user"
)
If you need a custom .user file you can always do it manually by saving the generated Visual Studio and creating a template from it similarly to the above example.

How to exclude .lib file from linker command line argument in VC++

I am compiling a vc++ program which is showing below error
error LNK1104: cannot open file 'D:\Visual Studio 2010\Projects\credentialproviders\Win32\Debug\Helpers.lib'
how i can remove that Helpers.lib file reference from linker command line argument because in project settings->linker->commandline its in readonly mode and I can't edit this to remove that line. I am using Visual Studio 2010.
thanks
Search the .vcproj file, and the project source code for "Helpers.lib". There are a couple places it can get linked in, and this is the simplest way to find it.
Otherwise, look for:
Project properties -> linker -> inputs
In the solution explorer, see if the .lib is included anywhere in the project tree.
Anywhere in the source code, #pragma comment(lib, "helpers.lib")
If Helpers is a project in your solution, check project dependencies for the project. By default, thisk linking is done implicitly if the project depends on it.
if this lib is not referenced in Linker->Input->Additional Dependencies, so check Project Dependencies, and remove any dependency on porject Helpers. also it's possible that this linking is done from code by #pragma comment(lib, "Helpers)
I don't really think the above answers it properly. I just had this same problem and the way to fix it is to go to View->Property Manager. double click Microsoft.Cpp.Win32.user and go to Linker->Input. There you can edit out the additional dependencies that were previously read only. Do it for both debug/release versions if needed.

Where do I input DLL dependencies in Visual Studio C++ project?

I am converting some Qt project files (.pro) that run on Linux and Mac into Visual Studio project files (.vcproj) The Qt Visual Studio add-in converted everything fine except the DLL dependencies. Where do I put these in Visual Studio 2008?
If I put the DLLs in Configuration Properties > Linker > Input > Additional Dependencies, I get:
fatal error LNK1107: invalid or corrupt file: cannot read at 0xABC
Where do dynamically-linked dependencies go?
Project Properties -> Linker -> Input -> Additional Dependencies
In that field put xxxx.lib for whatever library you need.
You might want to check what’s the differences between .dll , .lib, .h files ?.
You need to specify the corresponding .lib file at link time. not the dll.

Resources