一、 介紹預定義宏“__GNUC__”
一.1 __GNUC__ 是gcc編譯器編譯代碼時預定義的一個宏。
需要針對gcc編寫代碼時, 可以使用該宏進行條件編譯。
一.2 __GNUC__ 的值表示gcc的版本。
需要針對gcc特定版本編寫代碼時,也可以使用該宏進行條件編譯。
一.3 __GNUC__ 的類型是“int”
該宏被擴展后, 得到的是整數字面值。
可以通過僅預處理,查看宏擴展后的文本。見:《查看源文件預處理結果》
同時下面的示例也能體現出這一點。
二、 測試預定義宏__GNUC__
示例:
#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();
}
#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();
}
修改:
——2009/04/18
__GNUC__
__GNUC_MINOR__
__GNUC_PATCHLEVEL__
These macros are defined by all GNU compilers that use the C preprocessor:
C, C++, and Objective-C. Their values are the major version, minor version,
and patch level of the compiler, as integer constants. For example, GCC 3.2.1
will define __GNUC__ to 3, __GNUC_MINOR__ to 2, and __GNUC_PATCHLEVEL__ to
1. They are defined only when the entire compiler is in use; if you invoke the
preprocessor directly, they are not defined.
—— http://gcc.gnu.org/onlinedocs/gcc-3.1.1/cpp/Common-Predefined-Macros.html#Common%20Predefined%20Macros
相關鏈接:
——源代碼
http://immature.googlecode.com/svn/trunk/iMmature/sample/predefined_macro/extension/__GNUC__/
——《查看源文件預處理結果》
http://www.shnenglu.com/ownwaterloo/archive/2009/04/16/get_result_of_preprocessing.html
——《預定義_MSC_VER宏》
http://www.shnenglu.com/ownwaterloo/archive/2009/04/15/predefined_macro__MSC_VER.html

本作品采用知識共享署名-非商業性使用-相同方式共享 2.5 中國大陸許可協議進行許可。
轉載請注明 :
文章作者 - OwnWaterloo
發表時間 - 2009年04月16日
原文鏈接 - http://www.shnenglu.com/ownwaterloo/archive/2009/04/16/predefined_macro___GNUC__.html