VS: Use Absolute instead of Relative Paths for all AdditionalDependencies Entries in .vcxproj Files
MSBuild
has a limitation, throwing an error LINK : fatal error LNK1104: cannot open file '..\..\..\path\to\mylibrary.lib'
, if the NAME_OF_TARGET_TO_BUILD.vcxproj
file contains an entry in the <AdditionalDependencies>
tag:
<Project DefaultTargets="Build" ToolsVersion="16.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ProjectName>NAME_OF_TARGET_TO_BUILD</ProjectName>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Link>
<AdditionalDependencies>..\..\..\path\to\mylibrary.lib</AdditionalDependencies>
pointing to a .LIB file, which is relative to a folder more than "three levels up".
Running either of the two commands:
// Build via CMake
cmake --build <ABSOLUTE_PATH_TO_FOLDER_WITH_VS_SOLUTION_FILE> --config Debug --target <NAME_OF_TARGET_TO_BUILD>
// Build via MSBuild
msbuild /p:Configuration=Debug /p:Platform=x64 <ABSOLUTE_PATH_TO_FOLDER_WITH_VS_SOLUTION_FILE>\subfolder_1\subfolder_2\subfolder_3\NAME_OF_TARGET_TO_BUILD.vcxproj
throws the mentioned error.
The problem does not occur when:
- opening the solution file (
<ABSOLUTE_PATH_TO_FOLDER_WITH_VS_SOLUTION_FILE>\MY_PROJECT.sln
) in VS2019 and building it, or - if the path to the file is absolute, or
- if the path to the file is relative to a folder only two or one levels up
// Absolute path
<AdditionalDependencies><ABSOLUTE_PATH_TO_FOLDER_WITH_VS_SOLUTION_FILE>\path\to\mylibrary.lib</AdditionalDependencies>
// Only two levels up
<AdditionalDependencies>..\..\path\to\mylibrary.lib</AdditionalDependencies>
// Only one level up
<AdditionalDependencies>..\path\to\mylibrary.lib</AdditionalDependencies>
My suggestion is for CMake to always use absolute paths when creating the <AdditionalDependencies>
segment of the .vcxproj files.
Edited by Brad King