如何找到 Windows SDK 的 SetEnv.cmd / SetEnv.cmd 无法正常工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/887894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:22:57  来源:igfitidea点击:

How to find Windows SDK's SetEnv.cmd / SetEnv.cmd Does not work correctly

windowsmsbuildsdksetenv

提问by OregonGhost

We have a Team City Build Server running and want to compile a Visual C++ project. So far this would be easy, since I've setup our Windows Build Agent with the Windows SDK, but we don't have a solution / project file.

我们有一个 Team City Build Server 正在运行,并且想要编译一个 Visual C++ 项目。到目前为止,这很容易,因为我已经使用 Windows SDK 设置了我们的 Windows 构建代理,但我们没有解决方案/项目文件。

The project files are instead created with CMake. CMake seems to be a little bit dumb (can't generate Solution when Visual Studio is not installed), but with some tricks, I could get it to do it. The solution can then be built with MSBuild.

项目文件是使用 CMake 创建的。CMake 似乎有点愚蠢(未安装 Visual Studio 时无法生成解决方案),但是通过一些技巧,我可以让它做到这一点。然后可以使用 MSBuild 构建解决方案。

And here comes the problem. For this to work automatically, I need to call the Windows SDK's SetEnv.cmd. And I can't seem to find it automatically. It's in the bin sub directory of the Windows SDK, but neither bin nor the root are in the path, and the %mssdk%environment variable is set by the SetEnv.cmdand is not available beforehand!

问题来了。为了让它自动工作,我需要调用 Windows SDK 的 SetEnv.cmd。而且我似乎无法自动找到它。在windows SDK的bin子目录下,但是bin和root都不在路径中,%mssdk%环境变量是SetEnv.cmd设置的,事先不可用!

Adding the Windows SDK\bin dir to the PATH leads to SetEnv.cmd no longer working (exits with a message like The x86 compilers are not currently installedand Jump target Set_x86 not found.

将 Windows SDK\bin 目录添加到 PATH 导致 SetEnv.cmd 不再工作(退出并显示类似The x86 compilers are not currently installedJump target Set_x86 not found.

The start menu link is calling the SetEnv.cmd with the Windows SDK dir as working directory instead. But if I add the root directory to the PATH, Bin\SetEnv.cmd is not available.

开始菜单链接使用 Windows SDK 目录作为工作目录调用 SetEnv.cmd。但是如果我将根目录添加到 PATH 中,则 Bin\SetEnv.cmd 不可用。

How can I find SetEnv.cmd automatically? Even setting an environment variable to the full path of the setenv.cmd doesn't work, and when I define %mssdk% as the sdk dir, then call %mssdk%\bin\SetEnv doesn't work as well. I also tried to define %mssdk%, then cd %mssdk%, then calling bin\SetEnv. Also compilers not found in all these cases. It also doesn't work if I manually cd to the root or bin dir on a command line and then call SetEnv.cmd...

如何自动找到 SetEnv.cmd?即使将环境变量设置为 setenv.cmd 的完整路径也不起作用,当我将 %mssdk% 定义为 sdk 目录时,调用 %mssdk%\bin\SetEnv 也不起作用。我还尝试定义 %mssdk%,然后是 cd %mssdk%,然后调用 bin\SetEnv。在所有这些情况下也找不到编译器。如果我在命令行上手动 cd 到 root 或 bin 目录,然后调用 SetEnv.cmd ,它也不起作用...

The start menu link works fine though.

不过,开始菜单链接工作正常。

For the record, my solution for now, as strange as this is, is the following:

作为记录,我现在的解决方案虽然很奇怪,但如下:

I created a MSBuild file that creates the solution file with CMake on the command line, then invokes the created solution with a MSBuild task. The MSBuild file can be easily built from TeamCity, though I needed some additional tricks to satisfy CMake's stupid looking for the compiler, though I won't invoke itthing. Not really satisfying, but it works.

我创建了一个 MSBuild 文件,该文件在命令行上使用 CMake 创建解决方案文件,然后使用 MSBuild 任务调用创建的解决方案。MSBuild 文件可以从 TeamCity 轻松构建,尽管我需要一些额外的技巧来满足 CMake寻找编译器的愚蠢行为,尽管我不会调用它。不是很令人满意,但它有效。

采纳答案by Mac

My solution (sets %WindowsSdkPath%, so that SetEnv.cmdcould be found under %WindowsSdkPath%Bin\):

我的解决方案(设置%WindowsSdkPath%,因此SetEnv.cmd可以在 下找到%WindowsSdkPath%Bin\):

@ECHO OFF

IF "%WindowsSdkVersion%"=="" (
  CALL :SetWindowsSdkVersionHelper HKCU > nul 2>&1
  IF ERRORLEVEL 1 CALL :SetWindowsSdkVersionHelper HKLM > nul 2>&1
  IF ERRORLEVEL 1 GOTO ERROR_NOWSDK
)

CALL :SetWindowsSdkPathHelper > nul 2>&1
IF ERRORLEVEL 1 GOTO ERROR_NOWSDK
GOTO END

:SetWindowsSdkPathHelper
SET WindowsSdkPath=
FOR /F "tokens=1,2*" %%i in ('REG QUERY "HKLM\SOFTWARE\Microsoft\Microsoft SDKs\Windows\%WindowsSdkVersion%" /V InstallationFolder') DO (
    IF "%%i"=="InstallationFolder" (
        SET "WindowsSdkPath=%%k"
    )
)
IF "%WindowsSdkPath%"=="" EXIT /B 1
EXIT /B 0

:SetWindowsSdkVersion
CALL :GetWindowsSdkVersionHelper HKCU > nul 2>&1
IF ERRORLEVEL 1 CALL :GetWindowsSdkVersionHelper HKLM > nul 2>&1
IF ERRORLEVEL 1 EXIT /B 1
EXIT /B 0

:SetWindowsSdkVersionHelper
SET WindowsSdkVersion=
FOR /F "tokens=1,2*" %%i in ('REG QUERY "%1\SOFTWARE\Microsoft\Microsoft SDKs\Windows" /V "CurrentVersion"') DO (
    IF "%%i"=="CurrentVersion" (
        SET "WindowsSdkVersion=%%k"
    )
)
IF "%WindowsSdkVersion%"=="" EXIT /B 1
EXIT /B 0

:ERROR_NOWSDK
ECHO The Windows SDK %WindowsSdkVersion% could not be found.
EXIT /B 1

:END

I was inspired for this by the SetEnv.cmditself...

我受到了SetEnv.cmd它本身的启发......

回答by grundic

Mac, nice answer! Now I would like to run msbuild with my project file. But before I should run SetEnv.Cmd - right?

麦克,很好的答案!现在我想用我的项目文件运行 msbuild。但在我应该运行 SetEnv.Cmd 之前 - 对吗?

So, here we go:

所以,我们开始:

run_Macs_code.bat REM see above
call "%WindowsSdkPath%\bin\Setenv.cmd" /Release /x86 /xp
cd E:\client
msbuild client.proj

Now it's working :)

现在它正在工作:)