MSBuild C++ - command line - can pass defines?
Asked Answered
H

2

9

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.

Heraclitus answered 15/1, 2013 at 16:42 Comment(3)
I should add: is there a way to do the above without modifying the project or solution file?Heraclitus
The answer is Yes after all, see https://mcmap.net/q/427578/-how-to-set-preprocessordefinitions-as-a-task-propery-for-the-msbuild-taskFreeboard
possible duplicate of How to set PreProcessorDefinitions as a task propery for the msbuild taskFreeboard
M
7

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
Mcgovern answered 15/1, 2013 at 16:54 Comment(6)
Yes, you need to modify the project file to propagate the value of the MSBuild property $(ErrorLogLevel) to the C++ macro ERROR_LOG_LEVEL passed to the compiler via the /D option. But, you do not need to use the IDE: you can edit project files and targets/props files in any text editor of your choosing.Mcgovern
Thank you. Does the method: msbuild /p:ErrorLogLevel=5 MyProject.vcxproj require opening the project in MSVC, or any kind of modification to the MyProject.vcxproj file?Heraclitus
Okay. I don't have the option to edit the proj file. Read only.Heraclitus
You can create your own project file that includes the read only project file and specifies the additional options, assuming the read only project file does not do anything horrible like reset ClCompile.AdditionalOptions.Mcgovern
I'll mark your answer as accepted, but it appears that the answer for my specific case is basically no. Thank you for your help.Heraclitus
Sorry just to confirm: isn't there a way to pass a pre-processor definition from the command line without changing the project file? Something to say msbuild /p:ERROR_LOG_LEVEL=5 MyProject.vcxprojZiska
R
0

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\""

Rayon answered 7/4, 2019 at 12:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.