預定義__GNUC__宏
1 __GNUC__ 是gcc編譯器編譯代碼時預定義的一個宏。需要針對gcc編寫代碼時, 可以使用該宏進行條件編譯。
2 __GNUC__ 的值表示gcc的版本。需要針對gcc特定版本編寫代碼時,也可以使用該宏進行條件編譯。
3 __GNUC__ 的類型是“int”,該宏被擴展後, 得到的是整數字面值。可以通過僅預處理,查看宏擴展後的文本。
示例:
#include <assert.h>
#include <stdio.h>
#include <typeinfo>
#ifndef __GNUC__
#error sample for gcc compiler
#else
/* use gcc special extension: #warning , __attribute__, etc. */
#endif
int main()
{
printf("hello gcc %d\n",__GNUC__);
assert( typeid(__GNUC__)==typeid(int) );
printf("press Enter to exit\n");
(void)getchar();
}