How to build C#, C++ , C Solutions using MsBuild and VS2017 compiler via Batch File?

msbuild build all projects in solution
msbuildemitsolution
msbuild command line
build solution file using msbuild
msbuild target
build c# solution from command line
msbuild exclude project from build command line
compile sln file

I am building my solutions using MsBuild in a batch file as follows:

@echo off
set msBuildExe="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe"
set solutionsFile="C:\TestProject\mySln.sln"
rem Build the solutions:  
%msBuildExe% /t:Build /p:Configuration=Release /p:Platform=x64 %solutionsFile%  

Problem is that I have Multiple Versions of Visual Studio installed and I want to point MsBuild.exe to use the compiler of VS2017.

How can I do that?

You need two seperate things here: msbuild, to drive the build process, and the compiler/linker/... to perform the actual build phases. These are actually orthogonal (theoretically, not sure how far it goes in practice): msbuild which comes with VS2017 can be used to build a project using the compiler/linker from VS2013, as long as the environment is setup for those. So what you really need here is that last thing: you probably don't care what version of msbuild is used exactly, you want the correct C++/C# toolsets used.

The canonical way to do that is to run the .bat files supplied with each version of VS, accessible from the start menu as 'Developer Command Prompt'. That will also setup the path to msbuild so you don't need to hardocde that. (note these batchfiles also have arguments allowing to select different versions of toolsets etc but that's out of scope for this question)

Example for VS2017, no hardocded paths so should be reusable accross machines and enterprise/community/...:

@echo off
call "%VS150COMNTOOLS\Tools\VsDevCmd.bat"
set solutionsFile="C:\TestProject\mySln.sln"
msbuild /t:Build /p:Configuration=Release /p:Platform=x64 %solutionsFile%  

Use MSBuild.exe to build specific targets in solutions, NET Desktop · Desktop with C++ · Universal Windows Platform (UWP) How to: Build specific targets in solutions by using MSBuild.exe type MSBuild.exe <​SolutionName>.sln , where <SolutionName> corresponds to the file name of the solution or ' , replace them with an _ in the specified target name. Using MSBuild with build targets. Using MSBuild with build properties. Using MSBuild with the 64-bit compiler and tools. Using MSBuild with different toolsets. Adding MSBuild customizations. Using MSBuild with Build Targets. A build target is a named set of predefined or user-defined commands that can be executed during the build.

This should get you where your going

What's new in MSBuild 15

Changed path

MSBuild is now installed in a folder under each version of Visual Studio. For example, C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild. You can also use the following PowerShell module to locate MSBuild: vssetup.powershell.

Use the Microsoft C++ toolset from the command line, Use the Microsoft C++ compiler toolchain (MSVC) from the command line You can build C and C++ applications on the command line by using tools The vcvarsall.bat file and other Visual Studio command files can vary from Use MSBuild (msbuild.exe) and a project file (.vcxproj) to configure a build  Now, to build the application, use the project file that you just created. At the command prompt, type msbuild helloworld.csproj -t:Build. This builds the Build target of the Helloworld project file by invoking the Visual C# compiler to create the Helloworld application. Test the application by typing helloworld.

How to build C#, C++ , C Solutions using MsBuild and VS2017 compiler via Batch File?

According to the Visual Studio Blog MSBuild is now part of Visual Studio!:

Starting with Visual Studio 2013, the 2013 version of MSBuild will ship as a part of Visual Studio instead of the .NET Framework. This transition allows us to more rapidly evolve MSBuild.

We could to know the MSBuild ships as a part of Visual Studio instead of the .NET Framework. So, if you want to use Visual Studio 2017 compiler, you need to use the MSBuild from the Visual Studio 2017:

Path: C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\MSBuild.exe

Besides, you can also use the VsDevCmd.bat file to set the build environment, just like stijn said, if you execute that file, you do not need to specify the MSBuild path, you can execute MSBuild.exe directly.

Check the similar thread for some details.

Hope this helps.

Walkthrough: Using MSBuild, You can run MSBuild from Visual Studio, or from the Command Window. In this walkthrough, you create an MSBuild project file using Visual Studio. The project file is represented in Solution Explorer by the project node such as C++, you need to unload the project (right-click on the project file and  You edit the project file in Visual Studio, and use the Command Window to build the project and examine the results. Create an MSBuild project. The Visual Studio project system is based on MSBuild. This makes it easy to create a new project file using Visual Studio. In this section, you create a Visual C# project file.

MSBuild Command-Line Reference, When you use MSBuild.exe to build a project or solution file, you can include several switches to specify If you run MSBuild from a shell other than the Windows command prompt, lists of The tools include compilers such as csc.​exe and vbc.exe. -dl:MyLogger,C:\My.dll*ForwardingLogger,C:\Logger.dll. If you're working on a computer that has only the Windows Software Development Kit (SDK), you can use the C# compiler at the SDK Command Prompt, which you open from the Microsoft .NET Framework SDK menu option. You can also use MSBuild to build C# programs programmatically. For more information, see MSBuild.

Devenv command line switches, Devenv lets you set various options for the IDE, build projects, debug projects, and Use these switches to run the IDE from a script or a .bat file (such as a nightly For more information, see MSBuild command-line reference. This switch is installed with the Desktop development with C++ workload. MSBuild Use MSBuild (msbuild.exe) and a project file (.vcxproj) to configure a build and invoke the toolset indirectly. It's equivalent to running the Build project or Build Solution command in the Visual Studio IDE. Running MSBuild from the command line is an advanced scenario and not commonly recommended. DEVENV

[Solved] How to Compile C# Solution using command prompt , csc.exe <filename.cs> will compile cs file. devenv /build ConfigName [/project ProjName] [/projectconfig compile .net spulution from command prompt[^] NET\Framework\v4.0.30319\MSBuild.exe "C:\Documents and How to create a .​bat batch file that automatically compiles a C++ file with command  Sometimes you might need to build multiple solutions or complex project in particular sequence. In such case you can build your project or solutions using command line or batch file. In this post I will explain simple way to build project or solution using command line tool or creating batch file.

Comments
  • Then you will need to target the right ms build in the installation directory if my mind serves me correctly