As mention in this gcc patches, the std module is built in gcc development branch. I build from source and try to use it in cmake project but it show std module not found
.
This is my minimal example:
cmake_minimum_required(VERSION 3.30 FATAL_ERROR)
set(CMAKE_CXX_STANDARD 23)
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.30)
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD "0e5b6991-d74f-4b3d-a41c-cf096e0b2508")
endif()
set(CMAKE_CXX_STANDARD_REQUIRED OFF)
set(CMAKE_CXX_MODULE_STD 1)
project(module-test VERSION 0.1.0 LANGUAGES CXX)
add_executable(module-test main.cpp)
target_link_options(module-test PRIVATE -L/home/leenhawk/tools/gcc/lib -Wl,-rpath,/home/leenhawk/tools/gcc/lib)
set(CMAKE_VERBOSE_MAKEFILE ON)
import std;
int main(int, char**){
std::cout << "Hello, from module-test!\n";
}
I have found libstdc++.module.json
in the ~/tools/gcc/lib
, how to use it?
The teminal output:
cmake] CMake Error in CMakeLists.txt:
[cmake] The "CXX_MODULE_STD" property on the target "module-test" requires that the
[cmake] "__CMAKE::CXX23" target exist, but it was not provided by the toolchain.
[cmake] Reason:
[cmake]
[cmake] Toolchain does not support discovering `import std` support
[cmake]
[cmake]
[cmake] -- Generating done (0.0s)
I use CMake 3.31.1. And there is command line:
/home/leenhawk/tools/gcc/bin/g++ main.cpp -L/home/leenhawk/tools/gcc/lib -Wl,-rpath,/home/leenhawk/tools/gcc/lib -std=c++2b -fmodules-ts
In module imported at main.cpp:1:1:
std: error: failed to read compiled module: No such file or directory
std: note: compiled module file is ‘gcm.cache/std.gcm’
std: note: imports must be built before being imported
std: fatal error: returning to the gate for a mechanical issue
compilation terminated.
CMake 3.31.1 comes with 2 files Clang-CXX-CXXImportStd.cmake
and MSVC-CXX-CXXImportStd.cmake
but no GNU-CXX-CXXImportStd.cmake
. It will probably be added at some point in the future, or you could try writing one based on the 2 examples.
In the meantime, you have to compile the module by hand first. Something like (ignore the error message from the linker)
g++ -std=gnu++26 -fmodules -fsearch-include-path bits/std.cc
After which you can compile your file normally
g++ -std=gnu++26 -fmodules a.cc
The JSON file basically contains instructions so CMake (or other) can guess this first command.
This merge request seems to have added support in CMake for using import std
with GCC, so hopefully in the next release it will work as easily as with Clang/MSVC.