I'd like to pass the name of an include file as a compiler argument so that I can modify a large number of configuration parameters. However, my C++ build is via a makefile like process that removes quotes from arguments passed to the compiler and pre-processor. I was hoping to do something equivalent to
我想将包含文件的名称作为编译器参数传递,以便我可以修改大量配置参数。但是,我的C ++构建是通过类似于makefile的makefile来删除传递给编译器和预处理器的参数的引号。我希望做一些相当于的事情
#ifndef FILE_ARG
// defaults
#else
#include "FILE_ARG"
#endif
with my command line including -DFILE_ARG=foo.h. This of course doesn't work since the preprocessor doesn't translate FILE_ARG.
使用我的命令行,包括-DFILE_ARG = foo.h。这当然不起作用,因为预处理器不翻译FILE_ARG。
I've tried
#define QUOTE(x) #x
#include QUOTE(FILE_ARG)
which doesn't work for the same reason.
由于同样的原因不起作用。
For scripting reasons, I'd rather do this on the command line than go in and edit an include line in the appropriate routine. Is there any way?
出于脚本原因,我宁愿在命令行上执行此操作,也不要在相应的例程中编辑包含行。有什么办法吗?
4 个解决方案
#1
25
For adding quotes you need this trick:
要添加引号,您需要这个技巧:
#define Q(x) #x
#define QUOTE(x) Q(x)
#ifdef FILE_ARG
#include QUOTE(FILE_ARG)
#endif
#2
4
You can do
你可以做
#ifdef FILE_ARG
#include FILE_ARG
#endif
On the command line
在命令行上
$ gcc -DFILE_ARG="\"foo.h\"" ...
should do the trick.
应该做的伎俩。
#3
1
You can also try the -include switch.
您也可以尝试使用-include开关。
gcc manual:
-include file
Process file as if #include "file" appeared as the first line of the primary source file. However, the first directory searched for file is the preprocessor's working directory instead of the directory containing the main source file. If not found there, it is searched for in the remainder of the #include "..." search chain as normal. If multiple -include options are given, the files are included in the order they appear on the command line.*
处理文件好像#include“file”出现在主要源文件的第一行。但是,搜索文件的第一个目录是预处理器的工作目录,而不是包含主源文件的目录。如果在那里找不到,则在#include“...”搜索链的其余部分中搜索正常。如果给出了多个-include选项,则文件将按照它们在命令行中出现的顺序包含在内。*
#4
0
Works for me. Maybe you forgot to quote correctly in your Makefile?
适合我。也许你忘了在你的Makefile中正确引用?
$ cat example.c #include FILE_ARG $ cat test.h #define SOMETHING $ gcc -Wall -Wextra -W -DFILE_ARG=\"test.h\" -c example.c $
EDIT: The reason you might not be able to get quoting to work is because the preprocessor works in phases. Additionally I used "gcc (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2", results may vary between compilers.
编辑:你可能无法引用工作的原因是因为预处理器分阶段工作。另外我使用“gcc(Ubuntu / Linaro 4.5.2-8ubuntu4)4.5.2”,结果可能因编译器而异。
#1
25
For adding quotes you need this trick:
要添加引号,您需要这个技巧:
#define Q(x) #x
#define QUOTE(x) Q(x)
#ifdef FILE_ARG
#include QUOTE(FILE_ARG)
#endif
#2
4
You can do
你可以做
#ifdef FILE_ARG
#include FILE_ARG
#endif
On the command line
在命令行上
$ gcc -DFILE_ARG="\"foo.h\"" ...
should do the trick.
应该做的伎俩。
#3
1
You can also try the -include switch.
您也可以尝试使用-include开关。
gcc manual:
-include file
Process file as if #include "file" appeared as the first line of the primary source file. However, the first directory searched for file is the preprocessor's working directory instead of the directory containing the main source file. If not found there, it is searched for in the remainder of the #include "..." search chain as normal. If multiple -include options are given, the files are included in the order they appear on the command line.*
处理文件好像#include“file”出现在主要源文件的第一行。但是,搜索文件的第一个目录是预处理器的工作目录,而不是包含主源文件的目录。如果在那里找不到,则在#include“...”搜索链的其余部分中搜索正常。如果给出了多个-include选项,则文件将按照它们在命令行中出现的顺序包含在内。*
#4
0
Works for me. Maybe you forgot to quote correctly in your Makefile?
适合我。也许你忘了在你的Makefile中正确引用?
$ cat example.c #include FILE_ARG $ cat test.h #define SOMETHING $ gcc -Wall -Wextra -W -DFILE_ARG=\"test.h\" -c example.c $
EDIT: The reason you might not be able to get quoting to work is because the preprocessor works in phases. Additionally I used "gcc (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2", results may vary between compilers.
编辑:你可能无法引用工作的原因是因为预处理器分阶段工作。另外我使用“gcc(Ubuntu / Linaro 4.5.2-8ubuntu4)4.5.2”,结果可能因编译器而异。