spdlog icon indicating copy to clipboard operation
spdlog copied to clipboard

vs编译工作正常, g++编译报错

Open dragon-dan opened this issue 1 year ago • 5 comments

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

dragon-dan avatar Apr 30 '24 07:04 dragon-dan

Can you write problems in English? Issues in this repository use English.

tt4g avatar Apr 30 '24 10:04 tt4g

Can you write problems in English? Issues in this repository use English.

As you wish

dragon-dan avatar May 01 '24 01:05 dragon-dan

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)?

tt4g avatar May 01 '24 03:05 tt4g

#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

dragon-dan avatar May 01 '24 05:05 dragon-dan

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.

tt4g avatar May 01 '24 09:05 tt4g