3

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?

1 Answer 1

5

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)\""
Sign up to request clarification or add additional context in comments.

3 Comments

Alternatively, you could use the preprocessor to insert the quotes, which might be less hassle: #define STRINGIFY(x) STRINGIFY_(x) / #define STRINGIFY_(x) #x / #include STRINGIFY(HEADER)
That's true; I hadn't needed to use that, but it should work fine. And, as you say, it sidesteps quotes on the command line, which is helpful.
@rici Your blurb deserves to be an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.