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

spdlog 1.12.0 is incompatible with fmt/ranges.h of libfmt 10.1.0 #2894

Closed
ldeng-ustc opened this issue Oct 7, 2023 · 5 comments
Closed

spdlog 1.12.0 is incompatible with fmt/ranges.h of libfmt 10.1.0 #2894

ldeng-ustc opened this issue Oct 7, 2023 · 5 comments

Comments

@ldeng-ustc
Copy link

ldeng-ustc commented Oct 7, 2023

I use spdlog 1.12.0 and libfmt 10.1.1 to compile the following code:

#include "spdlog/spdlog.h"
#include "fmt/ranges.h"

int main() {
    spdlog::info("hello");
    return 0;
}

The error message is as follows:

In file included from test.cpp:3:
/usr/local/include/fmt/ranges.h:685:1: error: ‘FMT_BEGIN_EXPORT’ does not name a type
  685 | FMT_BEGIN_EXPORT
      | ^~~~~~~~~~~~~~~~
/usr/local/include/fmt/ranges.h:728:1: error: ‘FMT_END_EXPORT’ does not name a type; did you mean ‘FMT_MODULE_EXPORT’?
  728 | FMT_END_EXPORT
      | ^~~~~~~~~~~~~~
      | FMT_MODULE_EXPORT

If I swap the include order of spdlog.h and ranges.h, then I get the following link error:

test.cpp:(.text.startup+0xb1): undefined reference to `spdlog::details::log_msg::log_msg(spdlog::source_loc, fmt::v10::basic_string_view<char>, spdlog::level::level_enum, fmt::v10::basic_string_view<char>)'
collect2: error: ld returned 1 exit status

The program can be compiled normally when using libfmt 9.1.0. Turning on SPDLOG_FMT_EXTERNAL does not help with the problem.

gcc version: 11.1.0
cmake version: 3.16.3

@tt4g
Copy link
Contributor

tt4g commented Oct 7, 2023

The link error is probably because you are using a pre-built spdlog instead of a header-only.
In this case, SPDLOG_FMT_EXTERNAL is meaningless unless it is specified when building spdlog.

@ldeng-ustc
Copy link
Author

The link error is probably because you are using a pre-built spdlog instead of a header-only. In this case, SPDLOG_FMT_EXTERNAL is meaningless unless it is specified when building spdlog.

Yes, I turn on SPDLOG_FMT_EXTERNAL when building spdlog, but the problem still exists.

I write a CmakeLists file with cpm to reproduce this problem, but I also tried to manually compile and install spdlog (whether SPDLOG_FMT_EXTERNAL is turned on or off), but the problem still persists. So I think the problem may be irrelevant to whether using external fmt or not.

cmake_minimum_required(VERSION 3.14...3.22)

project( testspdlog VERSION 1.0 LANGUAGES CXX)
include(./CPM.cmake)

CPMAddPackage(
  NAME fmt
  GIT_TAG 10.1.1   # 9.1.0 OK
  GITHUB_REPOSITORY fmtlib/fmt
  OPTIONS "FMT_INSTALL YES"
)

CPMAddPackage(
  NAME spdlog
  GIT_TAG v1.12.0
  GITHUB_REPOSITORY gabime/spdlog
  OPTIONS "SPDLOG_INSTALL YES
           SPDLOG_FMT_EXTERNAL YES"
)

add_executable(${PROJECT_NAME} test.cpp)
target_link_libraries(${PROJECT_NAME} fmt::fmt spdlog::spdlog)

Also, it is fine to include only fmt/format.h instead of fmt/ranges.h, the compilation and linking can pass normally.

@tt4g
Copy link
Contributor

tt4g commented Oct 7, 2023

The package manager named conan seems to be able to build spdlog 1.12.0 with fmt 10.1.1.

https://github.com/conan-io/conan-center-index/blob/903e5b16ac431e4ae046f0a33a773b51998e126f/recipes/spdlog/all/conanfile.py#L56-L60

Tip
The #2694 patch seems to have been applied.

https://github.com/conan-io/conan-center-index/blob/903e5b16ac431e4ae046f0a33a773b51998e126f/recipes/spdlog/all/conandata.yml#L20-L25C22

I don't understand why a problem that does not occur in the package manager only occurs on your machine.

@ldeng-ustc
Copy link
Author

Thank you for your reply.

I tried Canon and successfully solved the problem. After checking my configuration more carefully, I found that the reason for the problem was that I used the Options option of CPMAddPackage incorrectly, which did not pass SPDLOG_FMT_EXTERNAL to the spdlog compilation, and I did not add dependencies, which may cause spdlog to be compiled before fmt.

For the error in my native machine, it may also due to my compilation order, I’m not sure. But after I completely deleted spdlog.a and all header files of spdlog, deleted the cmake build directory, and completely recompiled and installed spdlog, the problem was solved.

For the sake of people who may encounter similar problems in the future, I leave my CMakeLists that successfully compiled with CPM:

cmake_minimum_required(VERSION 3.14...3.22)

project( testspdlog VERSION 1.0 LANGUAGES CXX)
include(./CPM.cmake)

CPMAddPackage(
  NAME fmt
  GIT_TAG 10.1.1   # 9.1.0 OK
  GITHUB_REPOSITORY fmtlib/fmt
  OPTIONS "FMT_INSTALL YES"
)

CPMAddPackage(
  NAME spdlog
  GIT_TAG v1.12.0
  GITHUB_REPOSITORY gabime/spdlog
  OPTIONS "SPDLOG_INSTALL YES"
  "SPDLOG_FMT_EXTERNAL YES"
)

add_dependencies(spdlog fmt)

add_executable(${PROJECT_NAME} test.cpp)
target_link_libraries(${PROJECT_NAME} fmt::fmt spdlog::spdlog)

@walkthetalk
Copy link

I encountered the same problem

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