[英]What does this C code mean (G_GNUC_PRINTF)?
static void ddict_debug(const char* fmt, ...) G_GNUC_PRINTF(1, 2);
I found this in a .c file and I don't understand this line : Is there just one function declaration or two ? 我在.c文件中发现了这个并且我不理解这一行:是否只有一个函数声明或两个?
What does this code mean ? 这段代码是什么意思?
G_GNUC_PRINTF()
is a glib library preprocessor macro. G_GNUC_PRINTF()
是一个glib库预处理器宏。 For the gcc compiler it is define as follows (from glib-2.4.5/glib/gmacros.h
): 对于gcc编译器,它定义如下(来自
glib-2.4.5/glib/gmacros.h
):
#define G_GNUC_PRINTF( format_idx, arg_idx ) \
__attribute__((__format__ (__printf__, format_idx, arg_idx)))
From the gnome documentation : 从gnome文档 :
Expands to the GNU C format function attribute if the compiler is gcc.
如果编译器是gcc,则扩展为GNU C格式函数属性。 This is used for declaring functions which take a variable number of arguments, with the same syntax as printf().
这用于声明采用可变数量参数的函数,其语法与printf()相同。 It allows the compiler to type-check the arguments passed to the function.
它允许编译器对传递给函数的参数进行类型检查。
Place the attribute after the function declaration, just before the semicolon.
将函数放在函数声明之后,就在分号之前。
Parameters:
参数:
format_idx
: the index of the argument corresponding to the format string (The arguments are numbered from 1)format_idx
:格式字符串对应的参数索引(参数编号为1)
arg_idx
: the index of the first of the format argumentsarg_idx
:第一个格式参数的索引
Example 1: 例1:
static void ddict_debug(const char* fmt, ...) G_GNUC_PRINTF(1, 2);
// | | | |
// format string, format_idx = 1 ----+ | <----+ |
// format arguments, arg_idx = 2 ---------+ <-------+
Example 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 ----------------+ <-------+
Summary: 摘要:
Is there just one function declaration or two ?
是否只有一个函数声明或两个?
One printf()-like function is defined. 一个类似printf()的函数被定义。 The macro tells the compiler to type-check the arguments passed to the function.
宏告诉编译器对传递给函数的参数进行类型检查。
There is only one function declared here. 这里只声明了一个函数。 The declaration is
static void ddict_debug(const char* fmt, ...);
声明是
static void ddict_debug(const char* fmt, ...);
, the G_GNUC_PRINTF(1, 2)
part is likely a macro which expands to a compiler specific function annotations. ,
G_GNUC_PRINTF(1, 2)
部分可能是一个宏,它扩展为编译器特定的函数注释。 For example, gcc has it to validate printf
like function's arugments, in the case it would expand to __attribute__ ((format (printf, 1, 2)));
例如,gcc用它来验证
printf
就像函数的arugments一样,如果它会扩展为__attribute__ ((format (printf, 1, 2)));
For more details, refer to 有关更多详细信息,请参阅
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.