vs编译工作正常, g++编译报错
use spdlog header-only vs2022 ok,g++ 9.3.0 build error
spdlog-1.14.0/include/spdlog/spdlog.h:296:98: error: expected primary-expression before )' token 296 | (logger)->log(spdlog::source_loc{FILE, LINE, SPDLOG_FUNCTION}, level, VA_ARGS) | ^
need help
ps: SPDLOG_LOGGER_INFO(logger, "hello"); ==> SPDLOG_LOGGER_INFO(logger, "hello", 0); then g++ ok
Can you write problems in English? Issues in this repository use English.
Can you write problems in English? Issues in this repository use English.
As you wish
ps: SPDLOG_LOGGER_INFO(logger, "hello"); ==> SPDLOG_LOGGER_INFO(logger, "hello", 0); then g++ ok
The compiler prints the macro as the cause of the error, but I don't know if the code is really the cause.
Also, it doesn't make sense that adding an argument to the macro would resolve error expected primary-expression before )' token.
Can you provide the code from your project where the problem occurred (the minimal code that can reproduce the problem)?
#define s_info(fmt, ...) \
SPDLOG_LOGGER_INFO(multi_logger, fmt, __VA_ARGS__);
use once in the code after initialization s_info("hello");
windows build run ok, linux build error, I don't know if it's a compiler issue
This is a well-known problem that occurs when using __VA_ARGS__.
If none of the arguments corresponding to __VA_ARGS__ are passed when the macro is expanded, __VA_ARGS__ will expand to "nothing".
However, the "," before __VA_ARGS__ will remain, resulting in a syntax error.
In other words, your macro is resolved as follows:
// Before
s_info("hello");
// After
SPDLOG_LOGGER_INFO(multi_logger, fmt, );;
, ) is the cause of expected primary-expression before )' token reported by GCC.
There are workarounds, such as __VA_OPT__ added in C++20.
See the following URL for more information.