How do I compile a Visual Studio project from the command-line?
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).
Build project in 2017 Visual Studio from the command line?
You should use the MSBuild.exe or csc.exe instead of devenv.exe.
const string COMPILER = "PATH/TO/DEV/TOOLS/msbuild.exe";
// later in code
psinfo = new ProcessStartInfo(COMPILER, "PATH\TO\PROJECT\PROJECT_NAME.sln /t:Rebuild /p:Configuration=Release");
Compiling with devenv requires more parameters ( and i think it requires to add some informations about projects ):
psinfo = new ProcessStartInfo(DEVENVPATH, @"""c:\Projects\[--pathtoproject--].sln"" /build RELEASE");
Is there a way to get the build command line used by Visual Studio?
When you build a project or solution in Visual Studio, the entire command line used to run the compiler (csc.exe, including switches and parameters) is displayed in the Output window. If you don't see it there, check the Verbosity level by going to:
Options > Projects and Solutions > Build and Run > MSBuild project build output verbosity
(You may need to choose "Show all settings")
It has to be set to "Normal" or higher. Check this question and answer for more details:
To see all command line on output window while compiling
How to build Visual studio Setup project using command-line?
Have reproduced same issue in my machine. And resolve it by workaround from it3xl.
Create a Text Document file in desktop and copy the content from this xx.bat file into it. (for professional editon)
Save and rename the text document from xx.txt to
Test.bat
- Double click the
Test.bat
file, then this issue goes away when i rebuild them bydevenv
command.
Hope it helps.
Building VS Solution via command prompt
This can happen when Visual Studio is not installed on the machine on which you're attempting to build the project (e.g. a build agent).
Verify VS is installed, or if this isn't an option you could run the following command in the package manager console:
Install-Package MSBuild.Microsoft.VisualStudio.Web.targets
This package will update the .csproj file of your project to use the VS version of the targets if they exist, otherwise a local version contained in the packages folder.
Build a Visual Studio C++ project using the Command Line
I think you need to use the call
batch command to call VsDevCmd.bat
from inside your batch file.
"The CALL command will launch a new batch file context along with any specified parameters. When the end of the second batch file is reached (or if EXIT is used), control will return to just after the initial CALL statement."
Please see the reference here
Another suggestion is to enclose paths containing spaces inside double quotes, i.e.
"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\"
Automated unit tests build and run on commandline with Visual Studio solution
Build
Building a Visual Studio solution/project through the command line is done with msbuild.exe
. It works best to add the path of MSBuild to the PATH
environment variable.
MSBuild is usually installed somewhere in the Visual Studio folders. E.g. on my machine the path is as follows:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe
Build the solution containing all your projects as follows:
msbuild.exe Example.sln
# Or if you want to build a release version or add additional arguments
msbuild.exe Example.sln /property:Configuration=Debug
See MSBuild CLI Docs for more options.
Side Note: Jenkins has a msbuild plugin you can use with a build step called "Build a Visual Studio project or solution using MSBuild" (Important: this does not install MSBuild, it only provides a GUI to use MSBuild in a build plan).
Run Tests
To run the tests you have two options:
- Run each project's executable in your build pipeline and the executable's exit code will indicate the success/failure of the unit tests for that project. However, you will need to call each executable separately; or
- Use the
vstest.console.exe
in combination with the Google Test Adapter
You can use the Google Test Adapter the same way in which Visual Studio uses it when you click Test -> Run -> All tests
to discover & execute tests in your projects.
In my environment, vstest.console.exe
is located here:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\TestPlatform\vstest.console.exe
You also need to provide the path to the test adaptor. Then execute all the tests as follows:
# Assuming vstext.console.exe is included in the PATH
# and the current working directory is the relevant project executable
# output folder:
vstest.console.exe Project1.exe Project2.exe Project3.exe /TestAdapterPath:"<path to adapter>"
Once again the path is hidden somewhere in the Visual Studio Folders. I found it through searching for GoogleTestAdapter.TestAdapter.dll
. On my machine it is located in:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\drknwe51.xnq
Conclusion
Thus, an automated way to build and run GoogleTest unit tests, which are split over multiple projects, with the commandline can be performed in these two steps:
- Build the solution/projects using
msbuild.exe
- Run the tests using
vtest.console.exe
in combination with the Google Test Adapter
Related Topics
How to 'Cout' the Correct Number of Decimal Places of a Double Value
How to Add a Timed Delay to a C++ Program
Stack-Buffer Based Stl Allocator
C++ Convert Vector<Int> to Vector<Double>
How to Programmatically Gain Root Privileges
How to Find the Actual Path Found by Bfs
How to Leverage Qt to Make a Qobject Method Thread-Safe
How to Add a Library Path in Cmake
Winapi Sleep() Function Call Sleeps for Longer Than Expected
Confusion Between C++ and Opengl Matrix Order (Row-Major VS Column-Major)
Template Tuple - Calling a Function on Each Element
Error with Multiple Definitions of Function
Why Is It Illegal to Take the Address of an Rvalue Temporary
Why Doesn't Narrowing Conversion Used with Curly-Brace-Delimited Initializer Cause an Error