一、 介紹預(yù)定義宏“__GNUC__”
一.1 __GNUC__ 是gcc編譯器編譯代碼時(shí)預(yù)定義的一個(gè)宏。
需要針對(duì)gcc編寫(xiě)代碼時(shí), 可以使用該宏進(jìn)行條件編譯。
一.2 __GNUC__ 的值表示gcc的版本。
需要針對(duì)gcc特定版本編寫(xiě)代碼時(shí),也可以使用該宏進(jìn)行條件編譯。
一.3 __GNUC__ 的類(lèi)型是“int”
該宏被擴(kuò)展后, 得到的是整數(shù)字面值。
可以通過(guò)僅預(yù)處理,查看宏擴(kuò)展后的文本。見(jiàn):《查看源文件預(yù)處理結(jié)果》
同時(shí)下面的示例也能體現(xiàn)出這一點(diǎn)。
二、 測(cè)試預(yù)定義宏__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();
}
修改:
——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
相關(guān)鏈接:
——源代碼
http://immature.googlecode.com/svn/trunk/iMmature/sample/predefined_macro/extension/__GNUC__/——《查看源文件預(yù)處理結(jié)果》
http://www.shnenglu.com/ownwaterloo/archive/2009/04/16/get_result_of_preprocessing.html——《預(yù)定義_MSC_VER宏》
http://www.shnenglu.com/ownwaterloo/archive/2009/04/15/predefined_macro__MSC_VER.html

本
作品采用
知識(shí)共享署名-非商業(yè)性使用-相同方式共享 2.5 中國(guó)大陸許可協(xié)議進(jìn)行許可。
轉(zhuǎn)載請(qǐng)注明 :
文章作者 - OwnWaterloo
發(fā)表時(shí)間 - 2009年04月16日
原文鏈接 -
http://www.shnenglu.com/ownwaterloo/archive/2009/04/16/predefined_macro___GNUC__.html
posted on 2009-04-16 20:07
OwnWaterloo 閱讀(10272)
評(píng)論(0) 編輯 收藏 引用