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

What is the proper way to use fmt as a C++ 20 module? #4237

Open
yuannan opened this issue Nov 13, 2024 · 5 comments
Open

What is the proper way to use fmt as a C++ 20 module? #4237

yuannan opened this issue Nov 13, 2024 · 5 comments

Comments

@yuannan
Copy link

yuannan commented Nov 13, 2024

I've seen loads of different examples online, but nothing seems to be in the official documents.

Some people have suggested to wrap fmt in a module and include that, while others have directly included it as a subdirectory in CMake.

I'm a little bit lost as what is the officially supported way to do this.

I'm building a new logging library specifically targeting cpp 23+ using fmt and I would really appreciate some input.

https://gitlab.com/Simple-Cpp/SplLog

Currently it compiles just fine on NixOS, but that is using the older 10.2.0 version that Nix ships. If I try to include as a git submodule and a cmake subdirectory it seems to explode while looking for __fwd/string.

Trying to use 10.2.0 from git but matching to the version Nix ships brings it's own compile issues.

If you can guide me on how to do this, I'd be happy to update the docs to make it a bit easier for new users.

@vitaut
Copy link
Contributor

vitaut commented Nov 14, 2024

Thanks for the suggestion. We should add a section to the docs and in the meantime check out https://vitaut.net/posts/2023/simple-cxx20-modules/.

@vitaut
Copy link
Contributor

vitaut commented Nov 14, 2024

The docs probably belong here: https://github.com/fmtlib/fmt/blob/master/doc/get-started.md.

@yuannan
Copy link
Author

yuannan commented Nov 14, 2024

The docs probably belong here: https://github.com/fmtlib/fmt/blob/master/doc/get-started.md.

I've tried this exact tutorial but it only worked with make, but not ninja. My main project is built in ninja so I would prefer to keep it this way.

I'm using NixOS which might be part of the problem, how would I debug what is going wrong?

ninja
[1/6] Building CXX object CMakeFiles/hello.dir/hello.cc.o
FAILED: CMakeFiles/hello.dir/hello.cc.o 
/nix/store/irkh4bl62gwpbh8vsz9i2prlqxznlahw-clang-wrapper-18.1.8/bin/clang++  -I/tmp/fmt_mod/fmt/include -std=c++20 -MD -MT CMakeFiles/hello.dir/hello.cc.o -MF CMakeFiles/hello.dir/hello.cc.o.d -o CMakeFiles/hello.dir/hello.cc.o -c /tmp/fmt_mod/hello.cc
/tmp/fmt_mod/hello.cc:1:8: fatal error: module 'fmt' not found
    1 | import fmt;
      | ~~~~~~~^~~
1 error generated.
[2/6] Scanning /tmp/fmt_mod/fmt/src/fmt.cc for CXX dependencies
ninja: build stopped: subcommand failed.

@Arghnews
Copy link
Contributor

@yuannan I had a play with this and managed to get it working with a change:

Building with clang 18.1.8, cmake 3.31.0, c++ std c++20 or c++23 on Ubuntu, fmt trunk

Configure line:
cmake -GNinja -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_CXX_STANDARD=23 -DCMAKE_CXX_EXTENSIONS=OFF -Bbuild

However, I had to hack this if line:

fmt/CMakeLists.txt

Lines 74 to 77 in 9ced61b

if (FMT_USE_CMAKE_MODULES)
target_sources(${name} PUBLIC FILE_SET fmt TYPE CXX_MODULES
FILES ${sources})
else()

To if (FMT_USE_CMAKE_MODULES AND 0) so force it to take the else. It works without changes for make for me too, just like you mentioned

I don't know if this is a known bug, with ninja, cmake, or how fmtlib is setup, or some combo. From a cursory scan, fmtlib seems to do it right but I'm no expert with modules

@vitaut
Copy link
Contributor

vitaut commented Nov 15, 2024

Could be related to vitaut/modules#16.

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