如何检索 cl.exe 的路径? [英] How to retrieve the path to cl.exe?
问题描述
我想在 Visual Studio 中检索编译器 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?
推荐答案
cl.exe
通常位于 %VCINSTALLDIR%\bin\
.VCINSTALLDIR
环境变量默认未设置,但在打开 Visual Studio 的本机工具命令提示符时会设置.
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 位
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\SxS\VC7
如果 VS 安装在系统范围内HKEY_CURRENT_USER\SOFTWARE\Microsoft\VisualStudio\SxS\VC7
如果为当前用户安装了 VS
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 user
64 位
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VC7
如果 VS 安装在系统范围内HKEY_CURRENT_USER\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VC7
如果为当前用户安装了 VS
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 user
然后你有每个安装版本的字符串.这是我机器上的样子:
Then 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.
这篇关于如何检索 cl.exe 的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!