Is there a way to convert something like this:
#define ERROR_LOG_LEVEL 5
Into something that msbuild via command line will pass to its projects?
msbuild.exe {???}ERROR_LOG_LEVEL=5 target
I've read the responses to similar questions, and it looks like the answer is no, just want to double-check in case some genius has found a workaround.
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\""
来源:https://stackoverflow.com/questions/14342492/msbuild-c-command-line-can-pass-defines