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

后端 未结 11 1630
借酒劲吻你
借酒劲吻你 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:35

    I'm a little late to the party (only 4 years or so), but I just had to workaround this problem on a project, and stumbled across this question while searching for a fix. Our solution was to use an environment variable with /D defines in it, combined with the Additional Options box in visual studio.

    1. In Visual Studio, add an environment variable macro, $(ExternalCompilerOptions), to the Additional Options under project options->C/C++->Command Line (remember both Debug and Release configs)
    2. Set the environment variable prior to calling msbuild. Use the /D compiler option to define your macros
        c:\> set ExternalCompilerOptions=/DFOO /DBAR 
        c:\> msbuild
    

    Item #1 ends up looking like this in the vcxproj file:

        
          $(ExternalCompilerOptions) ... 
        
    

    This works for me with VS 2010. We drive msbuild from various build scripts, so the environment variable ugliness is hidden a bit. Note that I have not tested if this works when you need to set the define to specific value ( /DACTIVATE=1 ). I think it would work, but I'm concerned about having multiple '='s in there.

    H^2

提交回复
热议问题