gcc_编译器预定义宏_是否为空

问题:gcc编译器本身预定义了一些宏(通过 gcc -dM -E -xc /dev/null 可以看到),但是有些宏定义为空。程序中需要根据

(1)预定义中是否定义了该宏;

(2)该预定义宏是否为空;

从而执行不同的程序路径。

我给出自己的解法,可能不太好,但这是我所能想到的。(尝试下使用宏粘贴“##”来解决这个问题,但是没成功)

方法:如果该宏没有定义,任何程序员都可以解决;如果该宏定义不为空,则另定义一个宏,该宏的名称是在原宏名称的末尾加上“SEU__",否则不定义新的宏。

Makefile实现

CPPFLAGS_ADD_SEU = $(shell gcc -dM -E -xc /dev/null | grep $(1) | awk '{if(NF > 2)printf "-D" "%s" "SEU__", $$2}')

用法:
CPPFLAGS += $(call CPPFLAGS_ADD_SEU, __USER_LABEL_PREFIX__)
CPPFLAGS += $(call CPPFLAGS_ADD_SEU, __UINT_FAST8_TYPE__)

 

 

2014年10月15日补充:

今天看了下内核代码,因此有了以下记录:

查阅:IS_ENABLED          IS_BUILTIN              IS_MODULE

结合:include/config/auto.conf    &    include/generated/autoconf.h

/*
* Getting something that works in C and CPP for an arg that may or may
* not be defined is tricky. Here, if we have "#define CONFIG_BOOGER 1"
* we match on the placeholder define, insert the "0," for arg1 and generate
* the triplet (0, 1, 0). Then the last step cherry picks the 2nd arg (a one).
* When CONFIG_BOOGER is not defined, we generate a (... 1, 0) pair, and when
* the last step cherry picks the 2nd arg, we get a zero.
*/
#define __ARG_PLACEHOLDER_1 0,
#define config_enabled(cfg) _config_enabled(cfg)
#define _config_enabled(value) __config_enabled(__ARG_PLACEHOLDER_##value)
#define __config_enabled(arg1_or_junk) ___config_enabled(arg1_or_junk 1, 0)
#define ___config_enabled(__ignored, val, ...) val

 

/*
* IS_ENABLED(CONFIG_FOO) evaluates to 1 if CONFIG_FOO is set to 'y' or 'm',
* 0 otherwise.
*
*/
#define IS_ENABLED(option) \
(config_enabled(option) || config_enabled(option##_MODULE))

/*
* IS_BUILTIN(CONFIG_FOO) evaluates to 1 if CONFIG_FOO is set to 'y', 0
* otherwise. For boolean options, this is equivalent to
* IS_ENABLED(CONFIG_FOO).
*/
#define IS_BUILTIN(option) config_enabled(option)

/*
* IS_MODULE(CONFIG_FOO) evaluates to 1 if CONFIG_FOO is set to 'm', 0
* otherwise.
*/
#define IS_MODULE(option) config_enabled(option##_MODULE)

 

#define CONFIG_JERRY 2

int main(void)
{
int a = config_enabled(CONFIG_JERRY);
int b = ___config_enabled(xx 1, 0);
}

posted on 2013-09-05 08:52  阿加  阅读(2091)  评论(0)    收藏  举报

导航