How do I compile a Visual Studio project from the command-line?

I’m scripting the checkout, build, distribution, test, and commit cycle for a large C++ solution that is using Monotone, CMake, Visual Studio Express 2008, and custom tests.

All of the other parts seem pretty straight-forward, but I don’t see how to compile the Visual Studio solution without getting the GUI.

The script is written in Python, but an answer that would allow me to just make a call to: os.system would do.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I know of two ways to do it.

Method 1
The first method (which I prefer) is to use msbuild:

msbuild project.sln /Flags...

Method 2
You can also run:

vcexpress project.sln /build /Flags...

The vcexpress option returns immediately and does not print any output. I suppose that might be what you want for a script.

Note that DevEnv is not distributed with Visual Studio Express 2008 (I spent a lot of time trying to figure that out when I first had a similar issue).

So, the end result might be:

os.system("msbuild project.sln /p:Configuration=Debug")

You’ll also want to make sure your environment variables are correct, as msbuild and vcexpress are not by default on the system path. Either start the Visual Studio build environment and run your script from there, or modify the paths in Python (with os.putenv).

Method 2

MSBuild usually works, but I’ve run into difficulties before. You may have better luck with

devenv YourSolution.sln /Build

Method 3

To be honest I have to add my 2 cents.

You can do it with msbuild.exe. There are many version of the msbuild.exe.

C:WindowsMicrosoft.NETFramework64v2.0.50727msbuild.exe
C:WindowsMicrosoft.NETFramework64v3.5msbuild.exe
C:WindowsMicrosoft.NETFramework64v4.0.30319msbuild.exe
C:WindowsMicrosoft.NETFrameworkv2.0.50727msbuild.exe
C:WindowsMicrosoft.NETFrameworkv3.5msbuild.exe
C:WindowsMicrosoft.NETFrameworkv4.0.30319msbuild.exe

Use version you need. Basically you have to use the last one.

C:WindowsMicrosoft.NETFramework64v4.0.30319msbuild.exe

So how to do it.

  1. Run the COMMAND window
  2. Input the path to msbuild.exe

C:WindowsMicrosoft.NETFramework64v4.0.30319msbuild.exe

  1. Input the path to the project solution like

“C:UsersClark.KentDocumentsvisual studio
2012ProjectsWpfApplication1WpfApplication1.sln”

  1. Add any flags you need after the solution path.
  2. Press ENTER

Note you can get help about all possible flags like

C:WindowsMicrosoft.NETFramework64v4.0.30319msbuild.exe /help

Method 4

Using msbuild as pointed out by others worked for me but I needed to do a bit more than just that. First of all, msbuild needs to have access to the compiler. This can be done by running:

"C:Program Files (x86)Microsoft Visual Studio 12.0VCvcvarsall.bat"

Then msbuild was not in my $PATH so I had to run it via its explicit path:

"C:WindowsMicrosoft.NETFramework64v4.0.30319MSBuild.exe" myproj.sln

Lastly, my project was making use of some variables like $(VisualStudioDir). It seems those do not get set by msbuild so I had to set them manually via the /property option:

"C:WindowsMicrosoft.NETFramework64v4.0.30319MSBuild.exe" /property:VisualStudioDir="C:UsersAdministratorDocumentsVisual Studio 2013" myproj.sln

That line then finally allowed me to compile my project.

Bonus: it seems that the command line tools do not require a registration after 30 days of using them like the “free” GUI-based Visual Studio Community edition does. With the Microsoft registration requirement in place, that version is hardly free. Free-as-in-facebook if anything…

Method 5

MSBuild is your friend.

msbuild "C:path to solutionproject.sln"

Method 6

DEVENV works well in many cases, but on a WIXPROJ to build my WIX installer, all I got is “CATASTROPHIC” error in the Out log.

This works:
MSBUILD /Path/PROJECT.WIXPROJ /t:Build /p:Configuration=Release

Method 7

Both MSBuild and DevEnv are, rightfully, suggested in the answers here as the means to build from the command line. I wanted to point out that Microsoft says this about that: “In general, DEVENV is preferred over using MSBuild directly, because you can let Visual Studio handle the complexities of MSBuild.”

Reference

Method 8

dotnet build seems to be the newest way to build your solution.

https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-build


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x