4

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?

1 Answer 1

5

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.

1
  • 1
    Oh lord. That's it. Accepting this answer in 7 minutes! Thank u
    – Denis
    Commented Dec 10, 2023 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.