我有一个调试宏,可用于快速而肮脏的输出。我一直在尝试确定如何使代码在gcc 5.4上编译干净。在早期版本的gcc(4.x)或clang(11.0.3)上,它没有任何问题。错误是这样的:
main.c: In function ‘main’:
main.c:4:38: warning: too many arguments for format [-Wformat-extra-args]
do { if (1){ fprintf(stdout, "debug:%s:%04d:%s: " fmt, __FILE__, \
^
main.c:10:2: note: in expansion of macro ‘DEBUGPRINT’
DEBUGPRINT("How to have no arguments?\n", NULL);
我一直用来确定如何解决此问题的代码是:
#include <stdio.h>
#define DEBUGPRINT(fmt, ...) \
do { if (1){ fprintf(stdout, "debug:%s:%04d:%s: " fmt, __FILE__, \
__LINE__, __func__, __VA_ARGS__);} } while (0)
int main(int argc, char *argv[])
{
DEBUGPRINT("nums: %04i, %04i\n", 0x1234,0x5678);
DEBUGPRINT("How to have no arguments?\n", NULL);
return(0);
}
可以看到,如果我有参数,那没有问题。只有当我收到一条没有参数的消息时。我想我可以用'%s'传递一个“ \ n”,但是我只是好奇是否有办法处理NULL。
答案 0 :(得分:3)
fmt
参数必须省略,您需要将印刷的固定部分与传入的参数分开。
#include <stdio.h>
#define DEBUGPRINT(...) \
do { printf("debug:%s:%04d:%s: ", __FILE__, __LINE__, __func__);\
printf(__VA_ARGS__);\
} while (0)
int main(int argc, char *argv[])
{
DEBUGPRINT("nums: %04i, %04i\n", 0x1234,0x5678);
DEBUGPRINT("How to have no arguments?\n");
return(0);
}
输出:
debug:x1.c:0016:main: nums: 4660, 22136
debug:x1.c:0017:main: How to have no arguments?
答案 1 :(得分:1)
为了没有参数,您可以执行以下操作:
#include <stdio.h>
/* SEPARATE THE COMMA FROM THE __VA_ARGS__ macro with
* the ## token */
#define DEBUGPRINT(fmt, ...) \
do { if (1){ fprintf(stdout, "debug:%s:%04d:%s: " fmt, __FILE__, \
__LINE__, __func__,## __VA_ARGS__);} } while (0)
/* THIS TOKEN ELIMINATES THE COMMA ------------^^ */
int main(int argc, char *argv[])
{
DEBUGPRINT("nums: %04i, %04i\n", 0x1234,0x5678);
DEBUGPRINT("How to have no arguments?\n");
return(0);
}
我很惊讶它没有包含在标准中,但是它是CLANG和GCC编译器共享的扩展。
这是我系统上的输出:
$ a.out
debug:pru3225.c:0012:main: nums: 4660, 22136
debug:pru3225.c:0013:main: How to have no arguments?
$ _