cpreprocessorvariadic-macros

variadic macro doesn't compile


I have 2 variadic macros, one of them compiles fine and the other one doesn't:

    #define ASSERT(x, ...)           assert_log(x, __FILE__, __LINE__, __VA_ARGS__)
    #define TRACE (x, ...)           trace(x, __FILE__, __LINE__, __VA_ARGS__)
...

libs/defs.h:16:71: error: __VA_ARGS__ can only appear in the expansion of a C99 variadic macro [-Werror]
   16 |         #define TRACE (x, ...)           trace(x, __FILE__, __LINE__, __VA_ARGS__)
      |                                                                       ^

Their declared signatures:

void assert_log(int, const char*, int, const char* , ...);
void trace(int, const char*, int, const char* , ...);

Compilation flags:

CFLAGS= \
    -Wextra \
    -Werror \
    -Wall \
    -Wfloat-equal \
    -Wundef \
    -Wshadow \
    -Wcast-align \
    -Wstrict-prototypes \
    -Wswitch-enum \
    -Wformat=2 \
    -Werror=pointer-arith \
    -Wuninitialized \
    -pedantic \
    -std=c17 \
    -DDEBUG=1 \
    -g \
    -Og

Given the fact that their signature is the same, I don't understand why is GCC complaning?


Solution

  • I use Visual Studio 2022 C/C++, but I looked into your code. Please try:

    #define ASSERT(x, ...)    assert_log(x, __FILE__, __LINE__, __VA_ARGS__)
    #define TRACE(x, ...)     trace(x, __FILE__, __LINE__, __VA_ARGS__)
    

    Hope you see the difference. Its a small one. You added a space after TRACE and before the ( and that caused the error.