命令行编译VisualStudio

本文介绍了在Visual Studio 2010中使用devenv和MSBuild进行命令行编译的方法,并对比了两者之间的主要区别。包括如何编译单个project或solution,如何指定配置等。

  1. call "D:\Program Files\Microsoft Visual Studio 11.0\VC\vcvarsall.bat"  
  2. msbuild "WPEX.vcxproj" /p:Configuration=Debug /m  

使用devenv/MSBuild在命令行编译单个project

一 使用devenv来build单个project

devenv是VisualStudio的可执行程序,一般安装在“C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE”下。用来在命令行或GUI方式运行VisualStudio。其中devenv.com是命令行程序,devenv.exe是GUI的程序。默认地当你调用devenv的时候其实是启动devenv.com,除非你显示地调用devenv.exe才会启动GUI的VisualStudio。


使用devenv来build一个.sln的实例:
devenv d:\Build\MyProject\Src\MyProject.sln /Build "Release|Win32"  

 

使用devenv来build一个.sln中的某个的实例:
devenv.exe d:\Build\MyProject\Src\NyProject.sln /build "Release|Win32" /Project MyProject1 
注意:通常地.sln中的多个Projects间有依赖关系,所以虽然你只是build一个.sln中的某个Project,但是还是需要指定Project所在.sln,然后通过/Project来指定Project的名字。
 

如果只是单个的Project,没有引用其他的projects,这个时候可以不指定.sln,直接build Project,如下实例:

devenv d:\Build\MyProject\Src\MyProject.vcxproj /Build "Release|Win32"

注意此时实际上devenv做如下事:

此时devenv将在此project文件的父目录中查找与Project相同名字的.sln;
如果没有找到的话,然后查找其他的引用了此Project的.sln;
如果还是没有找到的话会创建临时的不保存的与Project同名的.sln。

 

二 devenv的更多帮助 

可以使用devenv /?来查看详细的帮助。
.sln或project的路径有空格时,需要对路径加"";
多个/开关间使用空格隔开;
devenv不支持通配符或正则表达式语法;

 

三 MSBuild

如果你的机器上没有装有VisualStudio,那么可以使用MSBuild来build .sln或project。MSBuild可以通过安装.NETFramework来安装,一般的安装路径为C:\Windows\Microsoft.NET\Framework。其实devenv执行build时候,后台也是调用MSBuild来build的。

 

可以使用msbuild /?来查看详细的帮助;

 

简单实例如下: 

MSBuild MyApp.sln /t:Rebuild /p:Configuration=Release
MSBuild MyApp.csproj /t:Clean
                     /p:Configuration=Debug /p:Platform=x86;TargetFrameworkVersion=v3.5

 

同样注意,如果project引用了其他的projects的时候,最好build整个.sln。 


Visual C++ Team Blog

C++ tutorials, C and C++ news, and information about the C++ IDE Visual Studio from the Microsoft C++ team.

Visual C++ Team Blog

VCBuild vs. C++ MSBuild on the Command Line

★ ★ ★ ★ ★
★ ★ ★ ★
★ ★ ★
★ ★
January 11, 2010 by  Visual CPP Team  //  12 Comments

In Visual Studio 2010, the command line tool vcbuild.exe will be replaced by msbuild.exe. The executable change does mean switches will change, too.  To help make the migration easier, I have created this table as a quick guide to the new switches and highlight some differences between the tools.  The table below isn’t a complete table of all the switches provided in both tools.

Migrating to MSBuild requires a new project type with a different extension (vcxproj).  Visual Studio comes with two tools for converting existing projects and solutions.  When dealing with a single project, the “vcupgrade.exe <filename>.vcproj” tool provides a quick conversion.  When dealing with multiple projects in a solution, use devenv to converts the whole solutions (.sln) and all of the projects within.  Once the project or solution has been converted without errors, you can use MSBuild.

When invoked with no explicit project configuration, VCBuild used to build all Configuration and Platform matrix by default, MSBuild, in contrast, builds only the default “Debug | Win32”.

In MSBuild, any feature that is enabled by /p[roperty] switch can also be enabled by setting the environment variable with the respective name.  For an example, “set Configuration=Debug” in the command line is the same as passing “/p:Configuration=Debug” to all MSBuild execution.

[] = optional characters to help remember the switch

Build Project

VCBuild.exe <projectname.vcproj>

MSBuild.exe <projectname.vcxproj>

Build Solution

VCBuild.exe <solutionname.sln>

MSBuild.exe <solutionname.sln>

Rebuild

/rebuild

/t[arget]:rebuild

Clean

/clean

/t[arget]:clean

Use Environment variables for INCLUDE and LIB

/useenv

/p[roperty]:useenv=true

Multi-processor build *

/m#

/m:#

Platform

/platform:<platform>

/p:Platform=<platform>

Configuration

<configuration>

/p:Configuration=<configuration>

Force Link (Link will always execute)

/forcelink

/t:BuildLink

Passes

/pass0

/t:BuildGenerateSources

 

/pass1

/t:BuildCompile

 

/pass2

/t:BuildLink /p:BuildType=Build

(BuildType property enables the incremental build tracking)

* Command line build are defaulted to use a single node (/m:1).  We encourage using n nodes where n is equal to the number of cores on the machine. 

MSBuild specific switches

Single File Build (Selected File Build)

Specify the files and tool’s target name that would execute upon the files. **

/t:<toolname> /p:SelectedFiles=”<files>”

 

/t:ClCompile

/p: SelectedFiles=”StdAfx.cpp;main.cpp”

Preprocess project file

Aggregate the project file by inlining all the files that would be imported during a build.  This doesn’t actually perform a build.

/PreProcess<:file> or /pp<:file>

 

/pp:outfile.txt

File logging

Logs the build into msbuild.log. See msbuild help for more information and options.

/FileLogger or /fl

Verbosity

Silence or increase details about the build.

Quiet and would barely show

/V[erbosity]:(q[uiet], m[inimal], n[ormal], d[etailed], or diag[nostic])

Detailed Summary

Provide statistics and summary at the end of build.

/DetailedSummary or /ds

 

** At this time, I can’t provide a list of tools, but there will be a complete list of possible targets on MSDN when it is ready.

If I have missed any options that you feel are important enough to go onto this list, please comment below.  I have left out many possible switches in order to emphasis the commonly used ones.  For further references, consider these links below:

MSBuild Command Line Reference – http://msdn.microsoft.com/en-us/library/ms164311.aspx

VC++ Building on the Command Line – http://msdn.microsoft.com/en-us/library/f35ctcxw%28VS.100%29.aspx

The New VC++ Project/Build system – https://channel9.msdn.com/posts/Charles/Bogdan-Mihalcea-The-New-VC-ProjectBuild-system-MSBuild-for-C/

Felix Huang

VC Project & Build Team

### 使用 Visual Studio 命令行工具 Visual Studio 提供了一系列强大的命令行工具来支持开发人员完成各种任务。这些工具允许开发者在不打开集成开发环境的情况下编译项目、运行构建脚本以及管理解决方案。 #### 启动 Developer Command Prompt 为了方便访问所有的命令行功能,安装 Visual Studio 之后会自动配置一个名为 "Developer Command Prompt for VS" 的快捷方式。通过这个提示符可以轻松调用 MSBuild、devenv.exe 和其他必要的工具[^1]。 ```bash # 打开开始菜单并找到 'Developer Command Prompt for VS' ``` #### 编译 C++ 项目 对于基于 Makefile 的工程,比如 `llama2.c` 文件,在 Visual Studio Code 中可以通过指定路径下的 make 工具来进行编译操作。而在原生的 Visual Studio 环境下,则通常依赖于 nmake 或者直接利用 devenv 来处理整个解决方案文件(.sln)。 ```cmd :: 在命令行编译单个C/C++源码文件 cl /EHsc /Fe:test.exe test.cpp :: 构建完整的解决方案 "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\devenv.com" MySolution.sln /build Release ``` #### 运行 Python 脚本并与调试器交互 当涉及到 Python 开发时,除了常规解释器外还可以借助 Visual Studio 自带的功能将调试器连接至正在运行的 REPL 实例上以便更好地诊断程序行为[^2]。 ```powershell # 启动Python交互模式,并准备接受来自VS的远程调试请求 python -m ptvsd --wait --port 5678 myscript.py ``` #### 设置线程名称(特定于 Xbox) 某些情况下可能需要强制设置线程名用于更详细的日志记录或是性能分析目的;然而需要注意的是此选项仅适用于 Xbox 平台上的应用程序开发,并且可能会干扰到 XDK COM 接口的工作机制因此需谨慎使用[^3]。 ```cpp // 宏定义控制是否开启线程命名特性 #define SETTHREADNAMES 1 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值