我想使用这样的宏参数:
I want to use macro parameter like this:
#define D(cond,...) do{ \ #if cond \ #define YYY 1 \ #else \ #define YYY 0 \ } while(0)有可能吗?
UPD 也许当源将被预处理两次时:gcc -E source.c | gcc -xc -接下来将起作用:
UPD Maybe when sources will be preprocessed twice: gcc -E source.c | gcc -xc - next will work:
#define D(cond,...) #define YYY cond&DEBUG #if YYY #define D(...) printf( __VA_ARGS__ ) #else #define D(...) #endif推荐答案
这是不可能的.了解有关 GNU cpp预处理器和C11标准(即 n1570 ),然后在此处. C预处理器(至少在概念上)在编译器的其余部分(得到编译器)之前运行翻译单元的预处理形式.顺便说一句,对于文件foo.c,您可以使用gcc -C -E foo.c > foo.i(使用 GCC )进入它的预处理形式,并且您可以使用分页器或编辑器检查foo.i-因为它是文本文件-.
This is not possible. Read about the GNU cpp preprocessor and the C11 standard (i.e. n1570), and check here. The C preprocessor is (conceptually at least) run before the rest of the compiler (which gets the preprocessed form of your translation unit). BTW, for a file foo.c you could use gcc -C -E foo.c > foo.i (using GCC) to get inside foo.i its preprocessed form, and you can inspect that foo.i -since it is a textual file- with a pager or an editor.
但是,可以生成.c文件(至少从1980年代开始,生成C代码是一种常见做法;例如,使用 yacc , bison , rpcgen , swig ,..;许多大型软件项目使用C或C ++代码的专用生成器...).您可能会考虑使用其他工具,例如 GPP 预处理器(或 GNU m4 )或其他程序或脚本来生成C文件(从其他文件).还可以查看 autoconf (它的目标可能与您的目标相似).
However, a .c file can be generated (generating C code is a common practice, at least since the 1980s; for example with yacc, bison, rpcgen, swig, ....; many large software projects use specialized generators of C or C++ code...). You might consider using some other tool, perhaps the GPP preprocessor (or GNU m4) or some other program or script, to generate your C file (from something else). Look also into autoconf (it might have goals similar to yours).
您可能想要为此目的配置构建自动化工具.编辑 Makefile /software/make/manual/html_node/index.html"rel =" nofollow noreferrer> GNU make .
You may want to configure your build automation tool for such purpose, e.g. edit your Makefile for GNU make.
更多推荐
是否可以在宏内部定义宏?
发布评论