2013-11-04 37 views
0

當我看了一些代碼,我發現以下快照。gcc擴展__attribute__含義

void ph_library_init_register(struct ph_library_init_entry *ent); 
#define PH_LIBRARY_INIT_PRI(initfn, finifn, pri) \ 
    static __attribute__((constructor)) \ 
    void ph_defs_gen_symbol(ph__lib__init__)(void) { \ 
    static struct ph_library_init_entry ent = { \ 
     __FILE__, __LINE__, pri, initfn, finifn, 0 \ 
    }; \ 
    ph_library_init_register(&ent); \ 
} 

我的問題是:1。 是什麼stribute手段? 2.代碼何時運行?

回答

0

在GCC __attribute__是類似於其他編譯器有形式__declspec,雖然也許有點更復雜(也GCC支持__declspec的兼容性)。

屬性可以應用於函數,變量和類型。他們可以改變事物「行爲」或「看起來像」的方式(例如對齊,可視性等)。

在您的特定情況下,__attribute__((constructor))意味着該函數將在main執行之前自動調用。在這種情況發生時並未指定,但您可以使用該屬性指定優先級以獲得對構造函數的相對順序的一些控制。

此功能的完整文檔位於:http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

(應當指出的是,這是一個宏定義,因此代碼實際上並不運行,除非您展開某些其他地方的PH_LIBRARY_INIT_PRI宏太)

+0

感謝。我知道了。 – buf1024