[ACCEPTED]-Using MSBuild to compile a single cpp file-msbuild

Accepted answer
Score: 10

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

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

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

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

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

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

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

Score: 1

You can do it like this (which is how VS 5 invokes MSBuild when you Ctrl+F7 on a file):

msbuild MyProject.vcxproj /t:ClCompile /p:SelectedFiles=MySourceFile.cpp

Note 4 that the SelectedFiles property needs to match the <ClCompile> item 3 in the .vcxproj, so it may be something 2 more like ..\path\to\MySourceFile.cpp, depending on how your files 1 are organized.

More Related questions