简体   繁体   English

如何在MSVS开发提示之外运行MSBuild?

[英]How to Run MSBuild Outside of the MSVS Dev Prompt?

I've recently been busy working on making proper build scripts for a code library I've been designing, and have been stuck on getting MSBuild to properly compile for my Windows builds. 我最近一直在忙于为我正在设计的代码库制作适当的构建脚本,并且一直坚持让MSBuild可以针对我的Windows构建进行正确编译。 What I'm trying to do is call MSBuild from a batch script without it opening the Visual Studio IDE, which it appears to constantly do if I'm not executing it from the Developer Command Prompt for Visual Studio. 我想做的是从批处理脚本调用MSBuild,而无需打开Visual Studio IDE,如果我不从Visual Studio的开发人员命令提示符执行它,它似乎会不断地执行此操作。 My script is simple enough, just calling 我的脚本很简单,只需调用

start C:\Program Files (x86)\...\MSBuild\15.0\Bin\MSBuild.exe <Project>.sln

Besides a few other specifiers tagged onto the end of it, that's what I'm trying to use to run my Windows builds. 除了在其末尾标记的其他一些说明符之外,这就是我试图用来运行Windows构建的内容。 The issue here is that whenever this code is called outside of the VS Dev Prompt, Visual Studio itself opens, not building the code at all. 这里的问题是,无论何时在VS Dev Prompt之外调用此代码,Visual Studio本身都会打开,而不是完全构建该代码。 I couldn't find anyone else struggling with this same issue either, as it seems to be new to the integration of MSBuild and Visual Studio. 我也找不到其他人为同一问题而苦苦挣扎,因为这似乎是MSBuild和Visual Studio集成的新内容。 Testing with older versions of MSBuild went to show that I could build projects as I wanted to outside of the Dev Prompt. 使用旧版本的MSBuild进行的测试表明,我可以根据需要在Dev Prompt之外构建项目。 Could I be missing some environment variable supplied in the Dev Prompt that changes the executable behavior? 我可能会缺少开发提示中提供的某些环境变量来更改可执行程序的行为吗? I couldn't seem to find any executable specifiers that would change this, either. 我似乎也找不到任何可以改变这种情况的可执行说明符。

Maybe I'm taking a completely wrong approach to this problem? 也许我对这个问题采取了完全错误的方法? My end goal is to provide consumers with a collection of build scripts, one for each platform they're targeting, so I am definitely open to other solutions. 我的最终目标是为消费者提供一组构建脚本,每个构建脚本针对的是他们所针对的平台,因此我绝对愿意接受其他解决方案。

The canonical answer: 规范答案:

You can use the devenv command with the /build command line option and the name of your project file. 您可以将devenv命令与/ build命令行选项以及项目文件的名称一起使用。

Alternatively, you can run one of the vcvars*.bat scripts to set up the necessary environment and then use msbuild. 或者,您可以运行vcvars * .bat脚本之一来设置必要的环境,然后使用msbuild。

For more info, see https://docs.microsoft.com/en-gb/cpp/build/building-on-the-command-line . 有关更多信息,请参见https://docs.microsoft.com/en-gb/cpp/build/building-on-the-command-line

Update: Contrary to the official advice above, this is what works on my laptop with VS2017 community edition. 更新:与上面的官方建议相反,这是我在带有VS2017社区版的笔记本电脑上可以使用的功能。

  1. Setup the environment by running the bat file that is targeted by the "developer command prompt for VS2017". 通过运行“ VS2017开发人员命令提示符”所针对的bat文件来设置环境。 (Right-click on that in the start menu, then select "Open file location" then right-click on the shortcut and select "properties". For me that is "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\Common7\\Tools\\VsDevCmd.bat". (在开始菜单中右键单击,然后选择“打开文件位置”,然后右键单击快捷方式并选择“属性”。对我来说,这是“ C:\\ Program Files(x86)\\ Microsoft Visual Studio \\ 2017 \\ Community \\ Common7 \\ Tools \\ VsDevCmd.bat”。

    You should call this file from your build script. 您应该从构建脚本中调用该文件。

  2. cd to your project directory and then use msbuild like this: cd到您的项目目录,然后使用msbuild像这样:

    msbuild My-project.vcxproj /p:Configuration=Release

For some really strange reason I can use the devenv method if I open the official developer command prompt but launching devenv from another command prompt even with the environment variables set does nothing. 出于某些非常奇怪的原因,如果我打开官方开发人员命令提示符,则可以使用devenv方法,但是即使设置了环境变量,也要从另一个命令提示符启动devenv,却没有任何作用。 (And I even compared the set of environment variables in both command prompts and they are equal.) (我什至在两个命令提示符下都比较了环境变量的集合,它们相等。)

How to Run MSBuild Outside of the MSVS Dev Prompt? 如何在MSVS开发提示之外运行MSBuild?

If the necessary environment not be set correct, then the build command becomes " mysolution.sln ". 如果未正确设置必要的环境,则构建命令将变为“ mysolution.sln ”。 And executing it indeed starts VS. 而执行它确实会启动VS。 So you should run the vcvars*.bat scripts to set up the necessary environment and then use msbuild. 因此,您应该运行vcvars*.bat脚本来设置必要的环境,然后使用msbuild。 Following is my a batch script, it has been working for some time, you can check it: 以下是我的批处理脚本,它已经运行了一段时间,您可以检查一下:

@echo OFF 
call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsDevCmd.bat"
echo "Starting Build for all Projects with proposed changes"

MSBuild.exe "C:\Users\Admin\Source\repos\MyTestProject\MyTestProject.sln"
pause
echo "All builds completed." 

Here is the test sample result: 这是测试样本结果:

在此处输入图片说明

See How to create a Simple Build Script for Visual Studio from the Command Line? 请参见如何从命令行为Visual Studio创建简单构建脚本? for more details. 更多细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM