简体   繁体   中英

include file with preprocessor define

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?

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)\""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM