c++clang++c++20c++-modules

Import std lib as modules with clang


I am experimenting with modules in clang, and would like to include the standard lib as modules instead of as includes.

Currently I do this

#include <iostream>
#include <string>

It seems that you in msvc should be able to import standard libs with for example

import std.core;

When using clang however this does not seem to be implemented, or implemented in another way.

My question is: Is it possible to import stl-includes like microsoft suggest, or is it possible to map standard lib includes to modules somhow.

Note: The reason I cannot use #include <...> or #import <...> is because of other errors that might get its own question. So I think that getting import std.core or similar is the way to go now if it is possible.

ModernesCpp also mentions std.core.


Solution

  • In Ubuntu-24.04+clang++-18

    // main.cpp
    import std;
    
    int main() 
    {
        std::print("Hello world!\n");
        return 0;
    }
    

    For above codes

    >>sudo apt install clang-18 libc++-18-dev
    >>clang++-18 -std=c++23 -stdlib=libc++  -Wno-reserved-module-identifier --precompile -o std.pcm /usr/lib/llvm-18/share/libc++/v1/std.cppm
    >>clang++-18 -std=c++23 -stdlib=libc++ -fmodule-file=std=std.pcm std.pcm main.cpp -o main
    >>./main
    Hello world!
    

    Using the C++23 std Module with Clang 18

    Modules in libc++