Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the path to cl.exe?

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?

like image 382
stefv Avatar asked Jun 07 '18 11:06

stefv


People also ask

Where can I find cl exe?

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.

Can not find cl exe?

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.

What is file cl exe?

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.

Where is Msvc installed?

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> .


1 Answers

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

  1. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\SxS\VC7 if VS is installed system-wide
  2. HKEY_CURRENT_USER\SOFTWARE\Microsoft\VisualStudio\SxS\VC7 if VS is installed for current user

64-bit

  1. HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VC7 if VS is installed system-wide
  2. HKEY_CURRENT_USER\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VC7 if VS is installed for current user

Then there you have strings for each installed version. This is how it looks on my machine:

VCINSTALLDIR value in registry

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.

like image 148
n0p Avatar answered Oct 20 '22 21:10

n0p