Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 with 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)\\""
like image 660
Jamie Cook Avatar asked Apr 01 '26 02:04

Jamie Cook


1 Answers

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.

like image 147
Anthony Williams Avatar answered Apr 02 '26 21:04

Anthony Williams