Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dragon-dan opened this issue Apr 30, 2024 · 5 comments
Closed

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

dragon-dan opened this issue Apr 30, 2024 · 5 comments

Comments

@dragon-dan
Copy link

dragon-dan commented Apr 30, 2024

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

@tt4g
Copy link
Contributor

tt4g commented Apr 30, 2024

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

@dragon-dan
Copy link
Author

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

As you wish

@tt4g
Copy link
Contributor

tt4g commented May 1, 2024

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

@dragon-dan
Copy link
Author

dragon-dan commented May 1, 2024

#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

@tt4g
Copy link
Contributor

tt4g commented May 1, 2024

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.

@gabime gabime closed this as completed May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants