msbuild: set a specific preprocessor #define in the command line

后端 未结 11 1639
借酒劲吻你
借酒劲吻你 2021-02-01 05:08

In a C++ file, I have a code like this:

#if ACTIVATE
#   pragma message( \"Activated\" )
#else
#   pragma message( \"Not Activated\")
#endif

I

11条回答
  •  清歌不尽
    2021-02-01 05:24

    C++ projects (and solutions) are not (yet ?) integrated in the MSBuild environment. As part of the build process, the VCBuild task is called, which is just a wrapper around vcbuild.exe.

    You could :

    • create a specific configuration for your solution where ACTIVATE=1 would be defined, and compile it with devenv.exe (with the /ProjectConfig switch).
    • create your own target file to wrap your own call to the VCBuild task (see the Override parameter)...
    • use vcbuild.exe instead of msbuild.exe. (vcbuild.exe does not seem to have the equivalent of an Override parameter).

    Note that your solution would not work for C# projects either unless you tweaked your project files a bit. For reference, here is how I would do this :

    • Add the following code before the call to :
    
      $(DefineConstants);$(MyConstants)
    
    • Call MSBuild like this :
    msbuild /p:MyConstants="ACTIVATE=1"

提交回复
热议问题