简体   繁体   English

使用gcc预处理器为#include定义文件名

[英]Use gcc preprocessor to define filename for #include

I would like to specify the name of a C include file at compile time as a C flag. 我想在编译时将C包含文件的名称指定为C标志。

eg 例如

#include MY_INCLUDE_FILE
int main(int argc, const char * argv[]) {...}

Would be expaned by the pre-processor to 预处理器将扩展为

#include "some_incfile.h"
int main(int argc, const char * argv[]) {...}

Doing something like this 做这样的事情

gcc -DMY_INCLUDE_FILE="some_incfile.h" main.c

I have attempted using the stringizing operator # to expand but have only gotten errors such as error: expected "FILENAME" or <FILENAME> 我尝试使用字符串化运算符#进行扩展,但仅出现诸如error: expected "FILENAME" or <FILENAME>

Is this even possible? 这有可能吗? -D define is not entirely necessary, the important part is that the include filename can be set from the gcc command line -D define并非完全必要,重要的是可以从gcc命令行设置include文件名

您必须转义“:

gcc -DMY_INCLUDE_FILE=\"some_incfile.h\" main.c

使用-include选项。

gcc -include "somefile.h" main.c

You can do it something like that 你可以那样做

#  if defined AAA
#define INC "x.h"
#elif defined BBB
#define INC "y.h"
#endif

#include INC

and from command line you do gcc -DAAA . 从命令行执行gcc -DAAA

and of course, you can pass directly gcc -DINC="\\"FILE.h\\"" if the file is really randomly generated from outside, by makefiles, etc. 当然,如果文件实际上是从外部,通过makefile等随机生成的,则可以直接传递gcc -DINC="\\"FILE.h\\""

Important is INC to be evaluated to a valid file name by the macro expansion procedure (see the Prosser's algorithm). 重要的是要通过宏扩展过程将INC评估为有效的文件名(请参阅Prosser的算法)。

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

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