How do I compile a single source file within an MSVC project from the command line?

為{幸葍}努か 提交于 2020-01-13 05:13:17

问题


I'm about to start doing some benchmarking/testing of our builds, and I'd like to drive the whole thing from a command line. I am aware of DevEnv but am not convinced it can do what I want.

If I could have a single file built within a single project, I'd be happy.

Can this be done?


回答1:


The magical incantation is as follows. Note that this has only been tested with VS 2010 - I have heard this is the first version of Visual Studio with this capability:

The Incantation

<msbuild> <project> <settings> <file>

Where

  • msbuild is a path to MSBuild.exe. Usually this should be set up for you by the VS2010 bat file so the right one will end up in your PATH, but if not I found one at C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe
  • project is the path to the vcxproj file within which your source file resides.
  • settings include the following:
    • /p:Configuration="Debug" // or whatever your Configuration is
    • /p:Platform=x64 // or x86
    • /t:ClCompile // to specify specifically you're looking to compile the file
  • file is actually another setting:
    • /p:SelectedFiles="path_to_file"

Notes

  • For <project> I had to specify a project (vcxproj) file instead of a solution (sln) file. The sln I would have used has multiple projects within it, so there would have been extra work to go that route anyhow (if it can even be done).
  • For the /p:Platform=x64 setting, there are several environment variables that pivot on what platform you are targeting (x64 v. x86) so make sure you set those up properly via Visual Studio's vcvarsall.bat.
  • Regarding path_to_file in the SelectedFiles parameter, this path must be the path as specified in the project file. If the path does not match the path used in the project file to reference the source, it doesn't seem to work.


来源:https://stackoverflow.com/questions/9285756/how-do-i-compile-a-single-source-file-within-an-msvc-project-from-the-command-li

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!