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

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

    I think you want:

    /p:DefineConstants=ACTIVATE
    
    0 讨论(0)
  • 2021-02-01 05:22

    The answer is : YOU CANNOT

    0 讨论(0)
  • 2021-02-01 05:23

    Use the CL environment variable to define preprocessor macros

    Before calling MSBUILD, simply set the environment variable 'CL' with '/D' options like so:

    set CL=/DACTIVATE to define ACTIVATE

    You can use the '#' symbol to replace '=' sign

    set CL=/DACTIVATE#1 will define ACTIVATE=1

    Then make the call to MSBUILD

    More documentation on the CL Environment Variables can be found at: https://msdn.microsoft.com/en-us/library/kezkeayy(v=vs.140).aspx

    0 讨论(0)
  • 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 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> :
    <PropertyGroup Condition=" '$(MyConstants)' != '' ">
      <DefineConstants>$(DefineConstants);$(MyConstants)</DefineConstants>
    </PropertyGroup>
    • Call MSBuild like this :
    msbuild /p:MyConstants="ACTIVATE=1"
    0 讨论(0)
  • 2021-02-01 05:25

    If you need to define some constant (not just true/false), you can do it the following way:

    On command line:

    MSBuild /p:MyDefine=MyValue
    

    In vcxproj file (in section <ClCompile>; and/or <ResourceCompile>, depending on where you need it):

    <PreprocessorDefinitions>MY_DEFINE=$(MyDefine);$(PreprocessorDefinitions)</PreprocessorDefinitions>
    

    Note that if you don't specify /p:MyDefine=MyValue in a call to MSBuild then empty string will be assigned to MY_DEFINE macro. If it's OK for you, that's it. If not, keep reading.

    How to make a macro undefined if corresponding MSBuild parameter is not specified

    To have MY_DEFINE macro undefined instead of empty string, you can use the following trick:

    <ClCompile>
      ....
      <PreprocessorDefinitions>_DEBUG;_CONSOLE;OTHER_UNCONDITIONAL_MACROS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <PreprocessorDefinitions Condition="'$(MyDefine)'!=''">MY_DEFINE=$(MyDefine);%(PreprocessorDefinitions)</PreprocessorDefinitions>
      ....
    </ClCompile>
    

    First PreprocessorDefinitions defines unconditional macros. Second PreprocessorDefinitions additionally defines MY_DEFINE macro when MyDefine is not empty string. You can test this by placing the following piece of code into your cpp file:

    #define STRINGIZE2(x) #x
    #define STRINGIZE(x) STRINGIZE2(x)
    
    #ifndef MY_DEFINE
    #pragma message("MY_DEFINE is not defined.")
    #else
    #pragma message("MY_DEFINE is defined to: [" STRINGIZE(MY_DEFINE) "]")
    #endif
    

    and running:

    > MSBuild SandBox.sln /p:Configuration=Debug /p:MyDefine=test /t:Rebuild
    ...
    MY_DEFINE is defined to: [test]
    ...
    
    > MSBuild SandBox.sln /p:Configuration=Debug /p:MyDefine= /t:Rebuild
    ...
    MY_DEFINE is not defined.
    ...
    
    > MSBuild SandBox.sln /p:Configuration=Debug /t:Rebuild
    ...
    MY_DEFINE is not defined.
    ...
    
    0 讨论(0)
  • 2021-02-01 05:26

    For VS2010 and up, see my answer here for a solution that requires no modification of the original project file.

    0 讨论(0)
提交回复
热议问题