使用 MSBuild 编译单个 cpp 文件 [英] Using MSBuild to compile a single cpp file

查看:31
本文介绍了使用 MSBuild 编译单个 cpp 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过从命令行调用 MSBuild 来构建整个项目:

I can build the whole project by calling MSBuild from the command line:

C:MyProject>MSBuild MyProject.vcproj

但是我没有找到有关编译单个文件的任何信息.本质上,我想做类似

However I didn't find any information about compiling a single file. In essence I would like to do something like

C:MyProject>MSBuild MyProject.vcproj /t:Compile:MySourceFile.cpp

我不想直接从命令行使用cl.exe",因为这会迫使我为 cl.exe 和所有环境变量定义所有相关的命令行选项,这是 MSBuild 已经为我完成的任务.

I do not want to directly use 'cl.exe' from the command line since that would force me to define all relevant command line options for cl.exe and all the environment variables, a task which MSBuild is already doing for me.

有什么办法可以实现吗?

Is there any way to achieve that?

请不要建议使用make"或ant"或其他任何东西,我特别需要 MSBuild.谢谢

And please, do not suggest using 'make' or 'ant' or whatever, I specifically need MSBuild. Thanks

推荐答案

VS2008 中的 MSBuild 使用 VCBuild 来完成实际工作,而 VCBuild 没有我所知道的构建单个文件的选项.(在 VS2010 中,这已经改变了,您实际上可以使用诸如 "/t:ClCompile "p:/SelectedFiles="main.cpp" 之类的东西来调用单个文件的编译)

MSBuild in VS2008 uses VCBuild to do the actual work, and VCBuild has no option I know of to build a single file. (with VS2010 this has changed, there you can actually invoke a compile of a single file using something like "/t:ClCompile "p:/SelectedFiles="main.cpp")

我可以想出一些肯定会奏效的想法,但需要一些额外的工作并且不是很简单:

I can come up with some ideas that will certainly work, but require some extra work and are not very straightforward:

  • 您可以通过 msbuild 调用 devenv 来编译单个文件:

  • you can msbuild have invoke devenv to compile a single file:

devenv myproject.sln /Command "File.OpenFile myfile.cpp" /Command "Build.Compile" /Command "File.Exit"

这确实会打开 IDE 窗口,并且很难确定编译是否真的成功.

this does open the IDE window though, and will make it pretty hard to figure out if the compile actually succeeded or not.

让 msbuild 调用一个脚本/程序来解析 vcproj 并制作一个副本,其中删除了源文件部分下的所有源,除了要编译的一个文件.然后让 msbuild 使用 vcbuild/pass1 构建该项目(pass1=仅编译,无链接).

have msbuild invoke a script/program which parses the vcproj and makes a copy with all sources under the Source File section removed except that one file you want to compile. Then have msbuild build that project using vcbuild /pass1 (pass1=compile only, no link).

始终保留与 vcproj 具有相同选项的响应文件,并让 msbuild 使用响应文件调用 cl 来编译单个文件.(制作响应文件就像在 VS 中打开项目属性一样简单,转到 C++->CommandLine 并复制列出的所有内容)

always keep a response file having the same options as your vcproj and let msbuild invoke cl to compile the single file, using the response file. (making the response file is as simple as opening the project properties in VS, going to C++->CommandLine and copying everything listed)

这篇关于使用 MSBuild 编译单个 cpp 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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