MSBuild C++ - command line - can pass defines?

主宰稳场 提交于 2019-12-04 05:19:25

Macros may be defined by passing the /D option to the compiler. You can specify the /D option from MSBuild using the AdditionalOptions of ClCompile:

<ItemDefinitionGroup>
    <ClCompile>
        <AdditionalOptions>/DERROR_LOG_LEVEL=5 %(AdditionalOptions)</AdditionalOptions>
    </ClCompile>
</ItemDefinitionGroup>

If you want to be able to pass the value for the macro via a call to msbuild.exe, you can easily do that too:

<ItemDefinitionGroup Condition="'$(ErrorLogLevel)' != ''">
    <ClCompile>
        <AdditionalOptions>/DERROR_LOG_LEVEL=$(ErrorLogLevel) %(AdditionalOptions)</AdditionalOptions>
    </ClCompile>
</ItemDefinitionGroup>

with msbuild.exe invoked as:

msbuild /p:ErrorLogLevel=5 MyProject.vcxproj

I've posted an answer in here, but I copy it for another answer. You need to define a user-defined macro in a PropertySheet. Then create a Preprocessor which refers to the user-defined macro. You can now use the new preprocessor value in your code. Finally, for the build, you can change the value of user-defined macro with /p flag. In here I defined a user-defined value like mymacro and a preprocessor value like VAL. Now you can simply compile the project with /p:mymacro="\"some thing new\"".

#include <iostream>


int main() {
    std::cout << VAL << std::endl;

    getchar();
}

yourproject.vcxproj:

<ClCompile>
  ...
  <PreprocessorDefinitions>VAL=$(mymacro);%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>

msbuild yourproject.vcxproj /p:mymacro="\"some thing new\""

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