如何从命令行为VS 2019构建C#程序? [英] How to build C# program from command line for VS 2019?

查看:665
本文介绍了如何从命令行为VS 2019构建C#程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用如下所示的脚本构建我的VS 2019 C ++项目:

I build my VS 2019 C++ projects with a script that looks like this:

call" C:\Program Files(x86)\ Microsoft Visual Studio \\ \\2019\Professional\VC\Auxiliary\Build\vcvarsall.bat" x64

msbuild.exe  Viewer2.sln / t:Clean,Build / p:Configuration = Release / p:Platform = x64



if NOT ["%errorlevel%"] == [" 0"]暂停

call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\vcvarsall.bat" x64
msbuild.exe  Viewer2.sln /t:Clean,Build /p:Configuration=Release /p:Platform=x64

if NOT ["%errorlevel%"]==["0"] pause

在另一台机器上,我只安装了带有C#工具的VS 2019。在那台机器上,Visual Studio 2019目录下没有vcvarsall.bat文件,所以我不知道如何构建项目的脚本,以便我知道正在使用
的msbuild.exe版本是正确的。我是否只需要对该机器上的msbuild.exe进行硬编码?这似乎不正确。

On another machine, I only have VS 2019 installed with C# tooling. On that machine there is no vcvarsall.bat file under Visual Studio 2019 directories, so I'm not sure how to script building the project so that I know that the version of msbuild.exe which is being used is correct. Do I have to just hard code the path the msbuild.exe on that machine? That doesn't seem correct.

推荐答案

嗨Benjamin Peikes,

Hi Benjamin Peikes,

欢迎来到MSDN论坛。

Welcome to MSDN forum.

>>如何从命令行为VS 2019构建C#程序?

>>How to build C# program from command line for VS 2019?

对于你的标题(问题),你可以使用命令喜欢:

For your title(question), you could use command like:


  • msbuild.exe TestBuild.sln / t:Clean,Build / p:Cpnfiguration = Release / p:Platform = x64

       实际上,您也可以使用以下格式:msbuild ... -t:xxx -p:xxx。它们具有相同的功能。

       Actually, you can also use format like: msbuild ... -t:xxx -p:xxx. They have same function.


  • msbuild xxx.sln =>构建整个解决方案,无论其中有多少项目

      msbuild xxx.csproj =>要构建单个项目,不会在同一个解决方案中构建其​​他项目

      msbuild xxx.csproj=>To build the single project, won't build other projects in same solution

有关msbuild的更多详细信息,请参阅
Msbuild options

More details about msbuild see Msbuild options.

>> 正在使用的msbuild.exe版本是正确的

从VS2010到VS2017,他们有VS版本的相应msbuild版本。

From VS2010 to VS2017, they have corresponding msbuild versions for VS version.

喜欢:VS2017-msbuild(15.x)VS2015-msbuild(14.x)...

Like: VS2017- msbuild (15.x) VS2015-msbuild(14.x) ...

对于VS2019,msbuild版本是16.x,打开Windows开始菜单,找到vs2019的开发人员命令提示符。打开它并键入msbuild,您可以获得如下版本信息:

For VS2019, the msbuild version is 16.x, open Windows Start menu and find the developer command prompt for vs2019. Open it and type msbuild you can get the version info like:

  (开发人员命令提示符:用于调用msbuild的好工具) 

  (developer command prompt:great tool used to call msbuild) 

版本信息:用于.NET Framework的Microsoft(R)Build Engine版本16.0.461 + g6ff56ef63c

>> 我是否只需要对该机器上的msbuild.exe进行硬编码?这似乎不正确。

取决于。构建C#程序:

# 1:如果你想使用像cmd.exe这样的东西,你必须输入它的完整路径,如:

#1:If you want use something like cmd.exe, you have to type the full path into it like:

C:\Program Files(x86)\ Microsoft Visual Studio \ 2019 \Professional\MSBuild\Current\Bin \MSBuild.exe xxx.sln xxx

C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\MSBuild.exe xxx.sln xxx

(这样,您可以添加环境变量的路径因此,每次要构建项目时都不需要键入完整路径。

(In this way, you can add the path to Environment Variable so that don't need to type the full path every time you want to build project)

#2:如果使用开发人员命令提示符,则只需使用如下命令:msbuild xxx。 sln或xxx.csproj xxx(开发人员命令提示符本身知道在哪里为您找到正确的msbuild版本,因此您无需输入路径)

#2:If you use developer command prompt, you can simply use command like: msbuild xxx.sln or xxx.csproj xxx (The developer command prompt itself knows where to find the correct msbuild version for you, so you don't need to enter the path)

>> < span style ="color:#666666; font-family:'Segoe UI',Helvetica,Garuda,Arial,sans-serif; font-size:14px">这似乎不正确。

我明白你的意思。但是在命令行中构建与编写代码有点不同。通过这种方式调用构建工具,使用硬代码完整路径查找msbuild.exe是正常的。 

I understand what you mean. But build in command-line is a bit different from writing code. In this way to call a build tool, using hard-code full path to find the msbuild.exe is normal. 

所以我建议你使用开发人员命令提示工具来构建C#和C ++本地。如果你使用jenkins Teamcity,VSTS等,并以硬编码方式配置完整路径。

So I suggest you use developer command prompt tool to build C# and C++ locally. And configure the full path in hard code way if you use jenkins Teamcity ,VSTS and so on.

希望以上所有有帮助。如果我的回复有帮助,我们会收到反馈意见:)

Hope all above helps. A feedback would be expected if my reply is helpful or not:)

最好的问候 

Best Regards 

Lance


这篇关于如何从命令行为VS 2019构建C#程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆