Search code examples
c++cmakec++-modulesc++23

How to use CMake to build a project with C++23 standard library module(import std) with Clang?


According to cppreference, the Standard Library Modules (P2465R3) has became an experimental feature in Clang 17 now. But I test it below, it failed:

CMakeList.txt

CMAKE_MINIMUM_REQUIRED(VERSION 3.27)

set(CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API "aa1f7df0-828a-4fcd-9afc-2dc80491aca7")
set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP ON)

set(CMAKE_CXX_EXTENSIONS OFF)

set(CMAKE_CXX_STANDARD 23)

project(1-1)

add_executable(${CMAKE_PROJECT_NAME})
target_sources(${CMAKE_PROJECT_NAME}
    PUBLIC
    FILE_SET all_my_modules TYPE CXX_MODULES FILES
    PUBLIC
    main.cpp
)

main.cpp

import std;
using namespace std;

int main(){
    cout<<"Hello\n";
}

It showed:

fatal error: module 'std' not found

So I guess it need to add extra option like <format> in Clang 14. But I can't find on llvm doc or google. Could someone give me some help?

I have tried clang's module in a more complex example but #include standard library. So I'm sure it's only because of Standard Library Modules.


Solution

  • The support for standard modules in clang and libc++ is highly experimental. Notably, if you just install it normally, the module files for the std module will not be installed.

    There are instructions provided by the libc++ team here, if you want to try it out:

    https://libcxx.llvm.org/Modules.html