Search code examples
linkerenvironment-variablesvisual-studio-2019pre-build-event

Is there a way to reference link.exe from Microsoft C++ toolset in Visual Studio 2019 without hardcode the path?


I am using link.exe from Microsoft C++ toolset in the pre-build event in a c# project in Visual Studio 2019.

The problem is every time the Visual Studio 2019 updates, it changes the path because of the version of MSVC folder.

For example: In VS 16.2.0 the path to link.exe changes from ..\14.21.27702... to ..\14.22.27905..

I search for an environment variable but I couldn't find it.

My full command line: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\bin\Hostx86\x86\link.exe" /MACHINE:x86 /VERBOSE /NOENTRY /DLL /OUT:"$(ProjectDir)$(OutDir)$(TargetName).ribbon.dll" "$(ProjectDir)RibbonMarkup.res")

I would like to be able to reference link.exe without this error-prone path.

Is there a way to get the path to link.exe without hardcoded it in my pre-build event?


Solution

  • After read more about Environments variables in MSVC here, and here

    I ran the command "SET" in "Tools-> Command Line-> Developer Command Prompt" and found the "VCToolsInstallDir" with the path I want.

    But in precompile events, this environment variable had no value set.

    So I performed: call "$(DevEnvDir)....\VC\Auxiliary\Build\vcvars32.bat" to set "VCToolsInstallDir" value.

    In the end, my pre-build event looked like this:

    call "$(DevEnvDir)..\..\VC\Auxiliary\Build\vcvars32.bat" "%VCToolsInstallDir%\bin\Hostx86\x86\link.exe" /MACHINE:x86 /VERBOSE /NOENTRY /DLL /OUT:"$(ProjectDir)$(OutDir)$(TargetName).ribbon.dll" "$(ProjectDir)RibbonMarkup.res"