Reputation: 963
I want to include a file and use a preprocessor definition for the path.
// in projects preprocessor definitions:
HEADER="../somePath/theHeader.h"
// in a file
#include HEADER
This works on Windows but XCode complains about this and can't find the file. Replacing HEADER
with the path works, so the file actually exists. So, what am I missing?
Upvotes: 3
Views: 2459
Reputation: 753675
What am I missing?
Enough quotes, probably. On Unix, you'd need:
HEADER = "../somePath/theHeader.h"
${CC} ${CFLAGS} -DHEADER='${HEADER}' -c file.cpp
The macro definition includes double quotes. If you don't wrap the -DHEADER
argument in single quotes, the shell (un)helpfully strips off the double quotes. By putting it inside single quotes, the shell helpfully removes the single quotes leaving the double quotes for the compiler to see.
The rules for command line processing are different on Windows and quotes are handled differently with the cmd.exe
processor.
Incidentally, when I want to make it feasible for a human to specify the value for HEADER
on the command line, I use:
HEADER = ../somePath/theHeader.h
${CC} ${CFLAGS} -DHEADER='"${HEADER}"' -c file.cpp
Now I can run:
make HEADER=/other/path/to/header.h file.o
and it works. With the original notation, I'd have to write:
make HEADER='"/other/path/to/header.h"' file.o
or something similar, which is fiddlier, and doubly awkward if you want to use a command output to specify the file name. Compare the first option with the second:
make HEADER=$(locate header.h)
make HEADER="\"$(locate header.h)\""
Upvotes: 5