views:

23

answers:

1

Hi all,

I'd like to pass a MSVC++ 2008 macro into my program via a /D define like so

/D__HOME__="\"$(InputDir)\""

then in my program I could do this

cout << "__HOME__ => " << __HOME__ << endl;

which should print somethine like

__HOME__ => c:\mySource\Directory

but it doesn't like the back slashes so I actually get:

__HOME__ => c:mySourceDirectory

Any thoughts on how I could get this to work?

UPDATE: I finally got this to work with Tony's answer below but note that the $(InputDir) contains a trailing back slash so the actual macro definition has to have an extra backslash to handle it ... hackery if ever I saw it!

/D__HOME__="\"$(InputDir)\\""
+2  A: 

You can convert your macro to a string by prefixing it with the stringizing operator #. However, this only works in macros. You actually need a double-macro to make it work properly, otherwise it just prints __HOME__.

#define STRINGIZE2(x) #x
#define STRINGIZE(x) STRINGIZE2(x)
cout<< "__HOME__ => " << STRINGIZE(__HOME__) << endl;

Incidentally macros containing double underscores are reserved to the implementation in C++, and should not be used in your program.

Anthony Williams
Hey thanks for the pointer Anthony and I've changed the define to HOME_DIR, however I get an "error C2014: preprocessor command must start as first nonwhite space" using your solution.
Jamie Cook
I've edited my comment to do it properly done with the double macro trick.
Anthony Williams