[英]What does this C code mean (G_GNUC_PRINTF)?
static void ddict_debug(const char* fmt, ...) G_GNUC_PRINTF(1, 2);
我在.c文件中發現了這個並且我不理解這一行:是否只有一個函數聲明或兩個?
這段代碼是什么意思?
G_GNUC_PRINTF()
是一個glib庫預處理器宏。 對於gcc編譯器,它定義如下(來自glib-2.4.5/glib/gmacros.h
):
#define G_GNUC_PRINTF( format_idx, arg_idx ) \
__attribute__((__format__ (__printf__, format_idx, arg_idx)))
從gnome文檔 :
如果編譯器是gcc,則擴展為GNU C格式函數屬性。 這用於聲明采用可變數量參數的函數,其語法與printf()相同。 它允許編譯器對傳遞給函數的參數進行類型檢查。
將函數放在函數聲明之后,就在分號之前。
參數:
format_idx
:格式字符串對應的參數索引(參數編號為1)
arg_idx
:第一個格式參數的索引
例1:
static void ddict_debug(const char* fmt, ...) G_GNUC_PRINTF(1, 2);
// | | | |
// format string, format_idx = 1 ----+ | <----+ |
// format arguments, arg_idx = 2 ---------+ <-------+
例2:
static void foo_debug(int foo, const char* fmt, ...) G_GNUC_PRINTF(2, 3);
// | | | | |
// not a printf argument --+ | | | |
// format string, format_idx = 2 -----------+ | <----+ |
// format arguments, arg_idx = 3 ----------------+ <-------+
摘要:
是否只有一個函數聲明或兩個?
一個類似printf()的函數被定義。 宏告訴編譯器對傳遞給函數的參數進行類型檢查。
這里只聲明了一個函數。 聲明是static void ddict_debug(const char* fmt, ...);
, G_GNUC_PRINTF(1, 2)
部分可能是一個宏,它擴展為編譯器特定的函數注釋。 例如,gcc用它來驗證printf
就像函數的arugments一樣,如果它會擴展為__attribute__ ((format (printf, 1, 2)));
有關更多詳細信息,請參閱
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.