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

查看:251
本文介绍了使用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.

有什么方法可以实现?

并且,请不要建议使用"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天全站免登陆