views:

45

answers:

3

In C++ CodeBlocks project I added the following definitions to the project settings, compiler settings, #define:

_DEBUG
DATA_DIR=\"/media/Shared/SiX/Data\"

This produces the following g++ command line:

g++ -Wall  -g -fPIC -save-temps -D_DEBUG -DDATA_DIR=\"/media/Shared/SiX/Data\"    -I../Includes  -c /media/Shared/SiX/SiXConfiguration/PathManager.cpp -o obj/Debug/PathManager.o

This code is not compiled:

char* commonDataDir;
#ifdef DATA_DIR
commonDataDir = DATA_DIR;
#endif

Looking at preprocessor output file, I see that source code line is expanded by this way:

commonDataDir = /media/Shared/SiX/Data;

I expect:

commonDataDir = "/media/Shared/SiX/Data";

The same code is compiled correctly from Eclipse CDT:

g++ -D_DEBUG -DDATA_DIR=\"/media/Shared/SiX/Data\" -I"/media/Shared/SiX (copy)/Includes" -O3 -Wall -c -fmessage-length=0 -fPIC -ggdb -MMD -MP -MF"PathManager.d" -MT"PathManager.d" -o"PathManager.o" "../PathManager.cpp"

So, the same command line parameter is handled differently by g++ proprocessor. How can I fix this?

A: 

You need to enclose the whole string in "

-DDATA_DIR="\"/media/Shared/SiX/Data\""
           ^                          ^
codaddict
Tried this, gives the same result as my version.
Alex Farber
A: 

This seems to fix it.

g++ -DDATA_DIR='"/media/Shared/SiX/Data"' ...
sharth
This change gives "No input files" g++ error message.
Alex Farber
+1  A: 

Putting quotes on macros is tricky and not a good idea.
Try and use the pre-processor to add the required quotes.

#define DO_QUOTE(X)       #X
#define QUOTE(X)          DO_QUOTE(X)

#ifndef DATA_DIR
#define DATA_DIR       /tmp
#endif

char commonDataDir[] = QUOTE(DATA_DIR);
Martin York
Thanks, this did the trick.
Alex Farber