Pass an absolute path as preprocessor directive on compiler command line
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 something 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 w开发者_运维问答ith Tony's answer below but note that the $(InputDir)
contains a trailing backslash so the actual macro definition has to have an extra backslash to handle it ... hackery if ever I saw it!
/D__HOME__="\"$(InputDir)\\""
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.
精彩评论