Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

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:
C++
#define LogErr(...) Log(ERR, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)

Because the message parameter is alway present, satisfying the ellipsis, this solves the problem.
 
Share this answer
 
v3
Comments
Andreas Gieriet 22-May-15 10:07am    
My 5!
This holds for *macros* only as given in the example above. The respective C-standard text is defined in 6.10.3, paragraph 4: [...] there shall be more arguments in the invocation than there are parameters in the macro definition (excluding the ...). [...].
Cheers
Andi
PS: For *functions* with variable arguments, there may be zero arguments, though.
Frankie-C 22-May-15 11:37am    
Thanks Andi
Add a dummy parameter (NULL) after the string.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900