158

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John Mulder
  • 9,765
  • 7
  • 33
  • 37

9 Answers9

151

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).

ks1322
  • 33,961
  • 14
  • 109
  • 164
Moses Schwartz
  • 6,879
  • 3
  • 21
  • 11
61

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

devenv YourSolution.sln /Build 
Jeffrey Hantin
  • 35,734
  • 7
  • 75
  • 94
  • MSBuild doesn't work with Visual Fortran projects ( *.vfproc ). Thanks for pointing me towards devenv, which does. – Maik Beckmann Oct 01 '10 at 12:44
  • 10
    You can also specify the config:`devenv YourSolution.sln /Build Debug` – Josh Stribling Feb 24 '12 at 23:57
  • 1
    Works great. One can also do rebuild using the below command `devenv YourSolution.sln /rebuild` – Abhineet Oct 15 '13 at 05:17
  • 2
    Also one can rebuild for release or debug mode by just appending debug or release after one space. For eg: `devenv YourSolution.sln /rebuild release` or `devenv YourSolution.sln /rebuild debug` – Abhineet Oct 15 '13 at 05:21
  • How do you target `.prj` project within `.sln `soluton? – Danijel Jul 03 '15 at 12:35
  • using cmake, the msbuild approach fails due to missing dependencies..likely vs env stuff i dont want to fight with. whereas this solution "just worked"(tm) thank you – Sass Nov 26 '17 at 10:00
  • If you are targeting C++ project, the following worked smoothly: devenv solutionname.sln /rebuild DEBUG /Project projectname – NucS Nov 26 '19 at 14:50
  • 1
    You can also specify the config with platform: ```devenv YourSolution.sln /Build "Debug|x64"``` – Ali Fallah Jan 05 '22 at 14:58
  • How do you get the command line to recognize devenv? I have Visual Studio 2022 with the C++ libraries installed, but when I open a PowerShell prompt and run `devenv`, it says `The term 'devenv' is not recognized as the name of a`... – Aaron Franke Jul 19 '23 at 22:40
31

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:\Windows\Microsoft.NET\Framework64\v2.0.50727\msbuild.exe C:\Windows\Microsoft.NET\Framework64\v3.5\msbuild.exe C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe
C:\Windows\Microsoft.NET\Framework\v2.0.50727\msbuild.exe C:\Windows\Microsoft.NET\Framework\v3.5\msbuild.exe C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe

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

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe

So how to do it.

  1. Run the COMMAND window

  2. Input the path to msbuild.exe

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe

  1. Input the path to the project solution like

"C:\Users\Clark.Kent\Documents\visual studio 2012\Projects\WpfApplication1\WpfApplication1.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:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe /help

NoWar
  • 36,338
  • 80
  • 323
  • 498
18

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.0\VC\vcvarsall.bat"

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

"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.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:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe" /property:VisualStudioDir="C:\Users\Administrator\Documents\Visual 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...

josch
  • 6,716
  • 3
  • 41
  • 49
6

MSBuild is your friend.

msbuild "C:\path to solution\project.sln"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adam Davis
  • 91,931
  • 60
  • 264
  • 330
  • 1
    I've seen that fail in odd situations due to differences in how MSBuild and DevEnv process .sln files. – Jeffrey Hantin Jan 31 '09 at 02:57
  • This is the ideal solution. But in our case we have a MSDEV (Visual Studio 2019) plug in that triggers a 2 pass build. This is for internal reasons but msbuild will only build a target 1 time. We have special files that require the EXE be built twice, once in a stub mode. Then a process runs the exe to create files used in pass2. – Ross Youngblood Jun 02 '22 at 16:22
3

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

peter.cyc
  • 1,763
  • 1
  • 12
  • 19
2

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

D.Howell
  • 31
  • 2
  • 3
    Maybe the article changed, but I followed your link and it doesn't say anywhere that DEVENV is preferred. And actually the [main page](https://learn.microsoft.com/en-us/visualstudio/ide/reference/devenv-command-line-switches?view=vs-2019) for DEVENV recommends using MSBUILD. – Jordan Ryder Oct 13 '21 at 15:40
  • 3
    Indeed! It would appear they took that statement out on that page. The Wayback Machine verifies this: https://web.archive.org/web/20210502101153/https://learn.microsoft.com/en-us/cpp/build/building-on-the-command-line?redirectedfrom=MSDN&view=msvc-160 – D.Howell Oct 14 '21 at 16:46
1

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

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

Rand Random
  • 7,300
  • 10
  • 40
  • 88
1

For build x64 platform targets with vs2019:

clean the projects

devenv *.sln /Clean "Release|x64"


build or rebuild the projects

devenv *.sln /Build "Release|x64"

devenv *.sln /Rebuild "Release|x64"

Walt White
  • 11
  • 2