gcc与__cplusplus宏

                     __cplusplus宏标志编译器会把代码按C还是C++语法来解析,gcc是没有定义改宏的,而g++却定义了。

[xxx@localhost test]$ 
[mapan@localhost test]$ ls
test.c
[xxx@localhost test]$ cat test.c 
#include<stdio.h>

#ifdef __cplusplus
int a=1;
#endif

int main()
{
   printf("a=%d\n",a);

   return 0;
}
[xxx@localhost test]$ gcc test.c 
test.c: In function ‘main’:
test.c:9: error: ‘a’ undeclared (first use in this function)
test.c:9: error: (Each undeclared identifier is reported only once
test.c:9: error: for each function it appears in.)
[xxx@localhost test]$ g++ test.c 
[xxx@localhost test]$ ./a.out 
a=1
[xxx@localhost test]$ 

结果一目了然。


上一篇:cplusplus


下一篇:c++ 2.0(c++11/14)