目录
参考资料
Preprocessor Options (Using the GNU Compiler Collection (GCC))
条件编译#ifdef的妙用详解_透彻_ifdef多个条件-CSDN博客
验证
直接用源码验证是最好的
源码
点击查看代码
#include <stdio.h>
// command: gcc -DAAA -DBBB=1 -DCCC=2 -DDDD=0 cMacroIfAndMacroIfdef.c
int main(int argc, char** argv)
{
#ifdef AAA
#pragma message("#ifdef AAA ok")
printf("#ifdef AAA ok\n");
#else
#pragma message("#ifdef AAA fail")
printf("#ifdef AAA fail\n");
#endif
#if AAA
#pragma message("#if AAA ok")
printf("#if AAA ok\n");
#else
#pragma message("#if AAA fail")
printf("#if AAA fail\n");
#endif
#if AAA == 0
#pragma message("#if AAA == 0 ok")
printf("#if AAA == 0 ok\n");
#else
#pragma message("#if AAA == 0 fail")
printf("#if AAA == 0 fail\n");
#endif
#if AAA == 1
#pragma message("#if AAA == 1 ok")
printf("#if AAA == 1 ok\n");
#else
#pragma message("#if AAA == 1 fail")
printf("#if AAA == 1 fail\n");
#endif
#if AAA == 2
#pragma message("#if AAA == 2 ok")
printf("#if AAA == 2 ok\n");
#else
#pragma message("#if AAA == 2 fail")
printf("#if AAA == 2 fail\n");
#endif
#ifdef BBB == 1
#pragma message("#ifdef BBB == 1 ok")
printf("#ifdef BBB == 1 ok\n");
#else
#pragma message("#if BBB == 2 fail")
printf("#if AAA ==2 fail\n");
#endif
#if BBB == 0
#pragma message("#if BBB == 0 ok")
printf("#if BBB == 0 ok\n");
#else
#pragma message("#if BBB == 0 fail")
printf("#if BBB == 0 fail\n");
#endif
#if BBB == 1
#pragma message("#if BBB == 1 ok")
printf("#if BBB == 1 ok\n");
#else
#pragma message("#if BBB == 1 fail")
printf("#if BBB == 1 fail\n");
#endif
#if BBB == 2
#pragma message("#if BBB == 2 ok")
printf("#if BBB == 2 ok\n");
#else
#pragma message("#if BBB == 2 fail")
printf("#if BBB == 2 fail\n");
#endif
#ifdef CCC
#pragma message("#ifdef CCC ok")
printf("#ifdef CCC ok\n");
#else
#pragma message("#ifdef CCC fail")
printf("#ifdef CCC fail\n");
#endif
#if CCC == 0
#pragma message("#if CCC == 0 ok")
printf("#if CCC == 0 ok\n");
#else
#pragma message("#if CCC == 0 fail")
printf("#if CCC == 0 fail\n");
#endif
#if CCC == 1
#pragma message("#if CCC == 1 ok")
printf("#if CCC == 1 ok\n");
#else
#pragma message("#if CCC == 1 fail")
printf("#if CCC == 1 fail\n");
#endif
#if CCC == 2
#pragma message("#if CCC == 2 ok")
printf("#if CCC == 2 ok\n");
#else
#pragma message("#if CCC == 2 fail")
printf("#if CCC == 2 fail\n");
#endif
#ifdef DDD
#pragma message("#ifdef DDD ok")
printf("#ifdef DDD ok\n");
#else
#pragma message("#ifdef DDD fail")
printf("#ifdef DDD fail\n");
#endif
#if DDD == 0
#pragma message("#if DDD == 0 ok")
printf("#if DDD == 0 ok\n");
#else
#pragma message("#if DDD == 0 fail")
printf("#if DDD == 0 fail\n");
#endif
#if DDD == 1
#pragma message("#if DDD == 1 ok")
printf("#if DDD == 1 ok\n");
#else
#pragma message("#if DDD == 1 fail")
printf("#if DDD == 1 fail\n");
#endif
#if DDD == 2
#pragma message("#if DDD == 2 ok")
printf("#if DDD == 2 ok\n");
#else
#pragma message("#if DDD == 2 fail")
printf("#if DDD == 2 fail\n");
#endif
#ifdef EEE
#pragma message("#ifdef EEE ok")
printf("#ifdef EEE ok\n");
#else
#pragma message("#ifdef EEE fail")
printf("#ifdef EEE fail\n");
#endif
#if EEE == 0
#pragma message("#if EEE == 0 ok")
printf("#if EEE == 0 ok\n");
#else
#pragma message("#if EEE == 0 fail")
printf("#if EEE == 0 fail\n");
#endif
#if EEE == 1
#pragma message("#if EEE == 1 ok")
printf("#if EEE == 1 ok\n");
#else
#pragma message("#if EEE == 1 fail")
printf("#if EEE == 1 fail\n");
#endif
#if EEE == 2
#pragma message("#if EEE == 2 ok")
printf("#if EEE == 2 ok\n");
#else
#pragma message("#if EEE == 2 fail")
printf("#if EEE == 2 fail\n");
#endif
return 0;
}%
编译效果
> gcc -DAAA -DBBB=1 -DCCC=2 -DDDD=0 cMacroIfAndMacroIfdef.c
cMacroIfAndMacroIfdef.c: In function ‘main’:
cMacroIfAndMacroIfdef.c:7:9: note: ‘#pragma message: #ifdef AAA ok’
7 | #pragma message("#ifdef AAA ok")
| ^~~~~~~
cMacroIfAndMacroIfdef.c:15:9: note: ‘#pragma message: #if AAA ok’
15 | #pragma message("#if AAA ok")
| ^~~~~~~
cMacroIfAndMacroIfdef.c:26:9: note: ‘#pragma message: #if AAA == 0 fail’
26 | #pragma message("#if AAA == 0 fail")
| ^~~~~~~
cMacroIfAndMacroIfdef.c:31:9: note: ‘#pragma message: #if AAA == 1 ok’
31 | #pragma message("#if AAA == 1 ok")
| ^~~~~~~
cMacroIfAndMacroIfdef.c:42:9: note: ‘#pragma message: #if AAA == 2 fail’
42 | #pragma message("#if AAA == 2 fail")
| ^~~~~~~
cMacroIfAndMacroIfdef.c:46:12: warning: extra tokens at end of #ifdef directive
46 | #ifdef BBB == 1
| ^~
cMacroIfAndMacroIfdef.c:47:9: note: ‘#pragma message: #ifdef BBB == 1 ok’
47 | #pragma message("#ifdef BBB == 1 ok")
| ^~~~~~~
cMacroIfAndMacroIfdef.c:58:9: note: ‘#pragma message: #if BBB == 0 fail’
58 | #pragma message("#if BBB == 0 fail")
| ^~~~~~~
cMacroIfAndMacroIfdef.c:63:9: note: ‘#pragma message: #if BBB == 1 ok’
63 | #pragma message("#if BBB == 1 ok")
| ^~~~~~~
cMacroIfAndMacroIfdef.c:74:9: note: ‘#pragma message: #if BBB == 2 fail’
74 | #pragma message("#if BBB == 2 fail")
| ^~~~~~~
cMacroIfAndMacroIfdef.c:79:9: note: ‘#pragma message: #ifdef CCC ok’
79 | #pragma message("#ifdef CCC ok")
| ^~~~~~~
cMacroIfAndMacroIfdef.c:89:9: note: ‘#pragma message: #if CCC == 0 fail’
89 | #pragma message("#if CCC == 0 fail")
| ^~~~~~~
cMacroIfAndMacroIfdef.c:96:9: note: ‘#pragma message: #if CCC == 1 fail’
96 | #pragma message("#if CCC == 1 fail")
| ^~~~~~~
cMacroIfAndMacroIfdef.c:100:9: note: ‘#pragma message: #if CCC == 2 ok’
100 | #pragma message("#if CCC == 2 ok")
| ^~~~~~~
cMacroIfAndMacroIfdef.c:108:9: note: ‘#pragma message: #ifdef DDD ok’
108 | #pragma message("#ifdef DDD ok")
| ^~~~~~~
cMacroIfAndMacroIfdef.c:115:9: note: ‘#pragma message: #if DDD == 0 ok’
115 | #pragma message("#if DDD == 0 ok")
| ^~~~~~~
cMacroIfAndMacroIfdef.c:125:9: note: ‘#pragma message: #if DDD == 1 fail’
125 | #pragma message("#if DDD == 1 fail")
| ^~~~~~~
cMacroIfAndMacroIfdef.c:132:9: note: ‘#pragma message: #if DDD == 2 fail’
132 | #pragma message("#if DDD == 2 fail")
| ^~~~~~~
cMacroIfAndMacroIfdef.c:140:9: note: ‘#pragma message: #ifdef EEE fail’
140 | #pragma message("#ifdef EEE fail")
| ^~~~~~~
cMacroIfAndMacroIfdef.c:144:9: note: ‘#pragma message: #if EEE == 0 ok’
144 | #pragma message("#if EEE == 0 ok")
| ^~~~~~~
cMacroIfAndMacroIfdef.c:154:9: note: ‘#pragma message: #if EEE == 1 fail’
154 | #pragma message("#if EEE == 1 fail")
| ^~~~~~~
cMacroIfAndMacroIfdef.c:161:9: note: ‘#pragma message: #if EEE == 2 fail’
161 | #pragma message("#if EEE == 2 fail")
| ^~~~~~~
运行效果
> ./a.out
#ifdef AAA ok
#if AAA ok
#if AAA == 0 fail
#if AAA == 1 ok
#if AAA == 2 fail
#ifdef BBB == 1 ok
#if BBB == 0 fail
#if BBB == 1 ok
#if BBB == 2 fail
#ifdef CCC ok
#if CCC == 0 fail
#if CCC == 1 fail
#if CCC == 2 ok
#ifdef DDD ok
#if DDD == 0 ok
#if DDD == 1 fail
#if DDD == 2 fail
#ifdef EEE fail
#if EEE == 0 ok
#if EEE == 1 fail
#if EEE == 2 fail
标签:AAA,定义,C语言,pragma,ifdef,fail,ok,message
From: https://www.cnblogs.com/SendBoringBackToNoWhere/p/17974617