Xenonic
Xenonic

Reputation: 364

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. 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. 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. 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. 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. Testing with older versions of MSBuild went to show that I could build projects as I wanted to outside of the 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.

Upvotes: 2

Views: 2571

Answers (2)

Sami Sallinen
Sami Sallinen

Reputation: 3494

The canonical answer:

You can use the devenv command with the /build command line option and the name of your project file.

Alternatively, you can run one of the vcvars*.bat scripts to set up the necessary environment and then use msbuild.

For more info, see https://learn.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.

  1. Setup the environment by running the bat file that is targeted by the "developer command prompt for VS2017". (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".

    You should call this file from your build script.

  2. cd to your project directory and then use msbuild like this:

    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. (And I even compared the set of environment variables in both command prompts and they are equal.)

Upvotes: 2

Leo Liu
Leo Liu

Reputation: 76996

How to Run MSBuild Outside of the MSVS Dev Prompt?

If the necessary environment not be set correct, then the build command becomes " mysolution.sln". And executing it indeed starts VS. So you should run the vcvars*.bat scripts to set up the necessary environment and then use 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:

enter image description here

See How to create a Simple Build Script for Visual Studio from the Command Line? for more details.

Upvotes: 1

Related Questions