带有宏的记录器的编译器错误 [英] Compiler error for logger with a macro

查看:88
本文介绍了带有宏的记录器的编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好



我需要帮助。



宏声明在头文件中,如下所示。

#define LogErr(message,...)Log(ERR,__ FILE__, __FUNCTION __,_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ $ $ $ $ $ $ $
。b。文件中的函数定义如下:/ b $ b void Log(char * logSeverity ,char * fileName,const char * functionName,int lineNo,char * message,...);





来自.C文件LogErr被调用两次,如下所示。当我编译时,我得到第二个调用错误,错误消息宏调用中的参数太少



LogErr(准备消息到%d%s ,6000,Hallo6);

LogErr(准备好消息);



如何解决问题?



谢谢

Merh

Hi

I need help.

Macro declaration is in a header file as below.
#define LogErr(message, ...) Log(ERR, __FILE__, __FUNCTION__, __LINE__, message, ##__VA_ARGS__)

Function Definition in a .C file is as below
void Log(char* logSeverity, char* fileName, const char* functionName, int lineNo, char* message,...);


From a .C file LogErr is called twice as below. When I compile I get error for the second called with the error message "too few arguments in macro invocation"

LogErr("Ready for messages to %d %s", 6000,"Hallo6");
LogErr("Ready for messages");

How can the problem be solved?

Thanks
Merh

推荐答案

ANSI要求elipsis(... )宏的声明接收至少一个参数。

虽然一些编译器也不接受变量参数的参数(即gcc),但并非所有人都这样。

最好解决方案是以这种方式修改宏:

ANSI requires that the elipsis (...) declaration of a macro receives at least one parameter.
While some compilers also accepts no arguments for variable arguments (i.e. gcc) this is not true for all.
The best solution is to modify the macro in this way:
#define LogErr(...) Log(ERR, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)



因为 message 参数总是存在,满足省略号,这解决了问题。


Because the message parameter is alway present, satisfying the ellipsis, this solves the problem.


添加一个虚拟参数( NULL )在字符串之后。
Add a dummy parameter (NULL) after the string.


这篇关于带有宏的记录器的编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆