I would like to retrieve the full path to the compiler cl.exe
in Visual Studio to call it from a program. Do we have keys in the registry for that? How to do it?
cl.exe is usually located at %VCINSTALLDIR%\bin\ . VCINSTALLDIR environment variable is not set by default, but it is being set when you open Visual Studio's Native Tools Command Prompt.
If you see the error "The term 'cl.exe' is not recognized as the name of a cmdlet, function, script file, or operable program.", this usually means you are running VS Code outside of a Developer Command Prompt for Visual Studio and VS Code doesn't know the path to the cl.exe compiler.
cl.exe is a tool that controls the Microsoft C++ (MSVC) C and C++ compilers and linker. cl.exe can be run only on operating systems that support Microsoft Visual Studio for Windows. You can start this tool only from a Visual Studio developer command prompt.
The command file location depends on the version of Visual Studio you installed, and on choices you made during installation. For Visual Studio 2019, the typical installation location on a 64-bit system is in \Program Files (x86)\Microsoft Visual Studio\2019\<edition> .
cl.exe
is usually located at %VCINSTALLDIR%\bin\
. VCINSTALLDIR
environment variable is not set by default, but it is being set when you open Visual Studio's Native Tools Command Prompt.
Here is how it is done in that batch script:
:GetVCInstallDir
@set VCINSTALLDIR=
@call :GetVCInstallDirHelper32 HKLM > nul 2>&1
@if errorlevel 1 call :GetVCInstallDirHelper32 HKCU > nul 2>&1
@if errorlevel 1 call :GetVCInstallDirHelper64 HKLM > nul 2>&1
@if errorlevel 1 call :GetVCInstallDirHelper64 HKCU > nul 2>&1
@exit /B 0
:GetVCInstallDirHelper32
@for /F "tokens=1,2*" %%i in ('reg query "%1\SOFTWARE\Microsoft\VisualStudio\SxS\VC7" /v "14.0"') DO (
@if "%%i"=="14.0" (
@SET VCINSTALLDIR=%%k
)
)
@if "%VCINSTALLDIR%"=="" exit /B 1
@exit /B 0
:GetVCInstallDirHelper64
@for /F "tokens=1,2*" %%i in ('reg query "%1\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VC7" /v "14.0"') DO (
@if "%%i"=="14.0" (
@SET VCINSTALLDIR=%%k
)
)
@if "%VCINSTALLDIR%"=="" exit /B 1
@exit /B 0
So depending on bitness of the system it looks at one of these registry keys
32-bit
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\SxS\VC7
if VS is installed system-wideHKEY_CURRENT_USER\SOFTWARE\Microsoft\VisualStudio\SxS\VC7
if VS is installed for current user64-bit
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VC7
if VS is installed system-wideHKEY_CURRENT_USER\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VC7
if VS is installed for current userThen there you have strings for each installed version. This is how it looks on my machine:
It requires an extra work to programmatically retrieve the correct value if you don't know what version you want, but that is out of scope of this question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With