7

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.
7
  • Please, add to the question post the exact compiler invocation (you may find it by calling make VERBOSE=1) and the exact error message it produces. Commented Nov 30, 2024 at 9:29
  • Note, that adding linker flags, like -L or -Wl, to the CMAKE_CXX_FLAGS variable is wrong: this variable is for compiler flags only. Commented Nov 30, 2024 at 9:30
  • Probably you need a version of cmake recent enough (may not exist yet?) that it has been taught about this new gcc feature. Commented Dec 1, 2024 at 10:41
  • @Tsyvarev I have changed my question. Commented Dec 1, 2024 at 10:41
  • @MarcGlisse But what should I use in command line? Commented Dec 1, 2024 at 10:51

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

3 Comments

GCC build 20241221 (on the way from GCC 14.2.1 to 15) seems to be happy also with using just C++23 standard (-std=c++23).
Indeed, 26 was just an example, not a requirement.
Finally, on CMake 4.0.0 rc3 the GCC cxx modules are fully supported, as there are three Std import files on cmake-4.0/Modules/Compiler: Clang-CXX-CXXImportStd.cmake; GNU-CXX-CXXImportStd.cmake; MSVC-CXX-CXXImportStd.cmake. Just tested it with gcc-15, it works perfectly!
3

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 main(){
  std::println("Hello world!");
}

Wonderful. Right?

4 Comments

Just tried this with gcc-15 on Ubuntu 24.04 and it worked fine! The only adjust I needed to do was that CMake 4.0 expected std.cc on /usr/lib/gcc/x86_64-linux-gnu/include/c++/15/bits/std.cc and I had it on /usr/include/c++/15/bits/std.cc. I tried to adjust it on CMake but couldn't, so I had to symlink the directory and then it worked fine: First,sudo mkdir -p /usr/lib/gcc/x86_64-linux-gnu/include/ Then, sudo ln -s /usr/include/c++/ /usr/lib/gcc/x86_64-linux-gnu/include/c++. In case someone else has the same issue on CMake 4.0.0 rc3.
@igormcoelho I ask this question on kitware gitlab, it's Ubuntu problem.... Distribution developer need to fix it.
That's true @Leen Hawk.. my only worry is that if this fix I propose is truly correct or not. Because I tried some mixing #include and import, and some compile on clang but breaks on gcc. Like: #include<string> \n import std; \n int main() { /* use string */ } Sometimes it breaks on gcc, and it references multiple definitions from different locations in my disk... clang compiles fine, but clangd also complains of ODR violation. I guess it could be some real ODR violation for gcc, it's hard to know for sure, or maybe just bug or misconfiguration. In any case, basic stuff is working fine.
Thanks for the advice @Leen Hawk... Just commented in Kitware gitlab and they contacted Ubuntu and GCC teams to fix this issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.