我在一个C项目中看到这行代码,没看懂。
#define FMT_CHK(fmt, args) __attribute__ ((format (printf, fmt, args)))
GNU官网上没有解释清楚(https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes)
__attribute__ ((format ())
的用途是什么,应该如何使用?
最佳答案
因此,fmt
和 args
参数只是告诉您哪个参数具有格式,哪个参数具有实参。
void myprintf(const char *fmt, ...);
// ^^ fmt = arg#1
// ^^ args = arg#2...
所以在这种情况下,正确的属性是:
__attribute__((format(printf, 1, 2)))
如果你有一个更长的函数声明...
void myprintf(obj *x, const char *fmt, int level, ...)
// ^^ format: arg#2
// ^^ args: arg#4...
__attribute__((format(printf, 2, 4)));
关于c - C中定义函数的__attribute__(格式)有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35279426/