这个C代码是什么意思(G_GNUC_PRINTF)?

Boh*_* LI 3 c static glib void

static void ddict_debug(const char* fmt, ...) G_GNUC_PRINTF(1, 2);
Run Code Online (Sandbox Code Playgroud)

我在.c文件中发现了这个,我不明白这一行:是否只有一个函数声明或两个?

这段代码是什么意思?

ser*_*gej 7

G_GNUC_PRINTF()是一个glib库预处理器宏.对于gcc编译器,它定义如下(from glib-2.4.5/glib/gmacros.h):

#define G_GNUC_PRINTF( format_idx, arg_idx )    \
  __attribute__((__format__ (__printf__, format_idx, arg_idx)))
Run Code Online (Sandbox Code Playgroud)

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 ---------+            <-------+
Run Code Online (Sandbox Code Playgroud)

例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 ----------------+            <-------+
Run Code Online (Sandbox Code Playgroud)

摘要:

是否只有一个函数声明或两个?

一个类似printf()的函数被定义.宏告诉编译器对传递给函数的参数进行类型检查.