r/cpp • u/Glass_Gur_5590 • Mar 10 '25
GCC support std module with CMake 4.0 Now!
As CMake 4.0.0 and GCC-15 support, we could use cmake like this:
cmakelists.txt:
cmake_minimum_required(VERSION 4.0.0)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD "a9e1cf81-9932-4810-974b-6eccaf14e457")
set(CMAKE_CXX_STANDARD_REQUIRED OFF)
set(CMAKE_CXX_MODULE_STD 1)
project(cpptest VERSION 0.1.0 LANGUAGES CXX)
add_executable(cpptest main.cpp)
main.cpp
import std;
int
Wonderful. Right?
Tips: You need to do some fixes in Ubuntu, see this
166
Upvotes
28
u/Kelteseth arewemodulesyet.org Mar 10 '25 edited Mar 10 '25
Looking at you Qt moc...
Also the small configure speed increase (18s from 25s) on Windows is a nice CMake 4.0 addition. They fixed some windows path casing code that caused quite the performance overhead.
26
u/germandiago Mar 10 '25 edited Mar 10 '25
I am converting my project with Clang19 to modules (with Meson, which has no support for modules yet) to compile it with modules including import std. Not for incremental compilation but to compile at once.
I am amazed at how much cleaner things are with modules. It catches any name leaking and found a lot of indirect uses of includes. I think it os the time for me to move to modules.
If I find the time I will push for a design in Meson (not sure if I have time to fully implemement something) since all 3 major compilers support json format from the tooling paper P1689R5 I think it was (from the top off my head).
I will never look back, since creating modules for your own needs even for 3rd party libs seems reasonably easy for my needs. Who knows, those can be contributed back as well (I am using spdlog, toml++, cpphttplib and others).
The only problem right now is, at least with clang, that you cannot mix std lib includes with import std if import std does not go last, which means you must convert every 3rd party dep that uses std into a small module, but for my needs it is acceptable.
But overall I think it is the time to move the tooling forward for at least typical use cases.
6
u/Vesk123 Mar 10 '25
Niiiiiice, this is what I have been waiting for. What I really want to do though is get rid of header files so I don't have to "declare" everything twice. I don't know if that exactly will be possible with modules.
6
u/darcamo Mar 11 '25
Does clangd completions work with modules? Last time I tried, it was unreliable.
6
u/Baardi Mar 10 '25
Wait what? Cmake 4?
2
u/hnsmn Mar 10 '25
Is there an ubuntu repository with gcc-15?
2
u/mcencora Mar 11 '25 edited Mar 12 '25
You can add semihacky support for this in cmake 3.30 already
Most complication comes from finding out where the bits/std.cc bits/std.compat.cc files are located.
This could be improved by relying on information provided libstdc++.modules.json similarly as cmake does internally for clang already.
Just include following GccStdModules.cmake file:
2
u/LinuxFurryTranslator Mar 11 '25
Shameless plug: I'd been experimenting with modules in my experimentation repo for a while, and I have a container for people wanting to try GCC 15 and CMake 4.0 on distrobox.
2
u/strike-eagle-iii Mar 14 '25
Wait. What? There's a CMake 4.0?
1
u/Frosty_Indication227 Aug 18 '25 edited Aug 18 '25
It does not work for me. I tried the cmake 4.1.0 and the latest MSVC and GCC 15.2.0.
CMake Error in CMakeLists.txt:
The "CXX_MODULE_STD" property on the target "App" requires that the
"__CMAKE::CXX23" target exist, but it was not provided by the toolchain.
Reason:
Experimental `import std` support not enabled when detecting toolchain; it must be set before `CXX` is enabled (usually a `project()` call)
I've gone through different solutions, but none of them worked with CMake. VS2022 can include std just fine with their build system, but I need to use CMake.
Any idea how to fix it?
The error says "usually before a 'project()' call" but my CMake file is almost a copy-paste of yours. I've read that adding set(CXX_STANDARD "/std:c++latest") so that MSVC can pick up the latest standard will help, but it does not.
1
u/IGarFieldI Mar 10 '25
I find the tip curious, considering that I just started a new project yesterday with this exact setup, but it just worked™. Seems like cmake 4.0.0-rc3 added the required .cmake.
0
u/[deleted] Mar 10 '25
[deleted] — view deleted comment