简体   繁体   English

包含文件的预处理器定义

[英]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. 这在Windows上有效,但是XCode对此抱怨,找不到文件。 Replacing HEADER with the path works, so the file actually exists. 用该路径替换HEADER可以工作,因此该文件实际上存在。 So, what am I missing? 那么,我想念什么?

What am I missing? 我想念什么?

Enough quotes, probably. 足够的报价,也许。 On Unix, you'd need: 在Unix上,您需要:

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. 如果您不将-DHEADER参数括在单引号中,则shell(un)会有效地去除双引号。 By putting it inside single quotes, the shell helpfully removes the single quotes leaving the double quotes for the compiler to see. 通过将其放在单引号中,shell可以帮助删除单引号,而双引号留给编译器查看。

The rules for command line processing are different on Windows and quotes are handled differently with the cmd.exe processor. Windows上命令行处理的规则不同,并且使用cmd.exe处理器对引号的处理方式也不同。

Incidentally, when I want to make it feasible for a human to specify the value for HEADER on the command line, I use: 顺便说一句,当我想让人们可以在命令行上指定HEADER的值时,我使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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