1

I want to specify a string parameter from MSBuild commandline.

I define a string type macro in c++ project like:

#define MYSTRING "hoge"

MYSTRING is assigned to a CString variable like:

CString str = MYSTRING;

And I want to pass this macro parameter via msbuild command line instead defining it on code.

I refered to this similar question, MSBuild C++ - command line - can pass defines? I added the parameter with /D option in the AdditionalOptions of ClCompile:

<ItemDefinitionGroup>
    <ClCompile>
        <AdditionalOptions>/D MYSTRING=$(MyString) %(AdditionalOptions)</AdditionalOptions>
    </ClCompile>
</ItemDefinitionGroup>

And then, with msbuild.exe invoked as:

msbuild /p:MyString="hoge" MyProject.vcxproj

But the error occured on line CString str = MYSTRING;:

error C2065: 'hoge': undeclared identifier

Do you know why this kind of error occurs ?

Is there any workaround?

television
  • 11
  • 2
  • You forgot the double quotes around the macro expansion to make it a string. – Some programmer dude Jul 12 '21 at 04:40
  • Oh, and remember that you probably have to escape the quotes, or the command line shell will remove them. – Some programmer dude Jul 12 '21 at 04:51
  • Thank you for your comments. I tried the same things as you mentioned but it didnt worked. Instead, I resolved this problem by doing like this: ```#define STRINGIZE2(x) #x #define STRINGIZE(x) STRINGIZE2(x)``` ```CString str = STRINGIZE(MYSTRING)``` – television Jul 13 '21 at 04:05
  • You're saying that `/D MYSTRING="$(MyString)"` didn't work? Or perhaps `/D MYSTRING=""$(MyString)""` (or similar)? How about `/p:MyString=""hoge""`? – Some programmer dude Jul 13 '21 at 05:05

0 Answers0