A couple months ago, I wrote about Using the C++23 std Module with Clang 18. Now that GCC 15 has been released, there is support for the C++23 std module in GCC as well.
We use the following minimal example for testing this feature:
import std; int main() { std::print("Hello world!\n"); }
Similar to Clang, some preparation is required to allow compiling this code. We need to compile the module interface:
g++ -std=c++23 -fmodules -fsearch-include-path -c bits/std.cc
The resulting std.o file is actually not required. What is important for us is the gcm.cache/std.gcm file that is generated by this compiler call. With this file present, we can now compile our code:
g++ -std=c++23 -fmodules -o test test.cpp
Note that we have to use the -fmodules
flag in both compiler calls with GCC, while this flag must not be used when using Clang.
Of course, all of this can be easily automated in a Makefile:
CXX = g++ CXXFLAGS += -Wall -Wextra -Wshadow -Wnon-virtual-dtor -pedantic CXXFLAGS += -std=c++23 -fmodules test: test.cpp gcm.cache/std.gcm $(CXX) $(CXXFLAGS) -o $@ $< gcm.cache/std.gcm: $(CXX) $(CXXFLAGS) -fsearch-include-path -c bits/std.cc
Sources: