正如这个gcc补丁中提到的,std模块是在gcc开发分支中构建的。我从源代码构建并尝试在 cmake 项目中使用它,但它显示
std module not found
。
这是我的最小示例:
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";
}
我在
libstdc++.module.json
中找到了~/tools/gcc/lib
,如何使用呢?
终端输出:
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)
我使用CMake 3.31.1。还有命令行:
/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 附带 2 个文件
Clang-CXX-CXXImportStd.cmake
和 MSVC-CXX-CXXImportStd.cmake
,但没有 GNU-CXX-CXXImportStd.cmake
。它可能会在将来的某个时候添加,或者您可以尝试根据这两个示例编写一个。
同时,您必须先手动编译模块。就像(忽略来自链接器的错误消息)
g++ -std=gnu++26 -fmodules -fsearch-include-path bits/std.cc
之后就可以正常编译你的文件了
g++ -std=gnu++26 -fmodules a.cc
JSON 文件基本上包含指令,因此 CMake(或其他)可以猜测第一个命令。