Search code examples
c++visual-studiomsbuildmsbuild-task

Where exactly is macro $(VCTargetsPath) defined?


We had a discussion before.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/e04e7791-c0c4-4598-b900-310878f5af45/how-can-i-locate-and-change-the-vctargetspath-variable?forum=msbuild

After hours searching .props files, all the $(VCTargetsPath) in .prop and .targets are deleted, but Visual Studio can still recognize this macro/variable correctly.

By modifing Microsoft.Cpp.ToolsetLocation.props, $(VCTargetsPath) can be modified and in consequence VS can no longer create new project.

So, where exactly this macro defined?

I believed that this macro is not and shouldn't be hardcoded.


Solution

  • Where exactly is macro $(VCTargetsPath) defined?

    Actually, VCTargetsPath property is defined in the MSBuild system props or targets files under MSBuild folder(C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild). And the files are nested and related to each other. Therefore, the values of various properties are referred to each other in the level-by-level files, and the value of VCTargetsPath is no exception.

    ==========================================================================

    MSBuild is actually equivalent to soft coding. Once MSBuild is installed, it exists as a bunch of dlls and various established props and targets files. You can modify its properties or extend its functions in various ways in the props or targets file. The MSBuild itself is an open source build tool. It is not as unchangeable as you think.

    ==========================================================================

    The VCTargetsPath is actually defined in the msbuild props file called Microsoft.Cpp.ToolsetLocation.props as you said.

    enter image description here

    It's just that the various targets and props files in MSBuild are nested and interrelated. VCTargetsPath is set as the value of _VCTargetsPathForToolset property while _VCTargetsPathForToolset is defined in another associated targets or props file.

    In general, use <import projects="xxxx\xxx.props or targets" /> to embed another file in the current targets or props file.

    And there are so many files in MSBuild that if you want to search layer by layer until you find the final specific value, it might seem a little complicated.

    Besides, VCTargetsPath is the path of some tools used by MSBuild to compile c++ projects.

    The default value in VS2019 is C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160.

    You can write a custom target in the xxx.vcxproj file to output its value.

    1) write this in xxx.vcxproj file:

    <Target Name="test123" AfterTargets="Build">
            <Message Importance="high" Text="$(VCTargetsPath)"></Message>
    </Target>
    

    2) Then rebuild your project and you can see this:

    enter image description here

    It is the MSBuild system defined path. If you change it easily, it will cause the project to fail to compile, unless you have a set of self-defined system similar to MSBuild to build C++ projects, and then put it in new path, let the new path overwrite VCTargetsPath property.

    In addition, I am curious why you want to find the lowest VCTargetPath value. If you want to know the principle of it, the above answer explains it.

    And if you want to overwrite its value, you can just create an environment system variable called VCTargetsPath directly, and then set its value to a new one.

    Restart VS to enable such new value. It is much easier than modifying the targets file.