是否可以在宏内部定义宏?

编程入门 行业动态 更新时间:2024-10-25 17:14:33
本文介绍了是否可以在宏内部定义宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用这样的宏参数:

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

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.

更多推荐

是否可以在宏内部定义宏?

本文发布于:2023-08-02 18:00:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279931.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定义

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!