問題:gcc編譯器自己預約義了一些宏(經過 gcc -dM -E -xc /dev/null 能夠看到),可是有些宏定義爲空。程序中須要根據程序員
(1)預約義中是否認義了該宏;shell
(2)該預約義宏是否爲空;ui
從而執行不一樣的程序路徑。this
我給出本身的解法,可能不太好,但這是我所能想到的。(嘗試下使用宏粘貼「##」來解決這個問題,可是沒成功)lua
方法:若是該宏沒有定義,任何程序員均可以解決;若是該宏定義不爲空,則另定義一個宏,該宏的名稱是在原宏名稱的末尾加上「SEU__",不然不定義新的宏。spa
Makefile實現code
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日補充:blog
今天看了下內核代碼,所以有了如下記錄:ip
查閱:IS_ENABLED IS_BUILTIN IS_MODULEget
結合: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);
}