How to build C#, C++ , C Solutions using MsBuild a

2019-08-16 17:45发布

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?

3条回答
劳资没心,怎么记你
2楼-- · 2019-08-16 17:53

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.

查看更多
Summer. ? 凉城
3楼-- · 2019-08-16 17:54

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%  
查看更多
家丑人穷心不美
4楼-- · 2019-08-16 18:01

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.

查看更多
登录 后发表回答