Skip to content

C++ 23 module support & import std #21143

Closed

Description

@MatthieuMv

When building with C++ 23 standard modules, the '-fbuiltin-module-map' clang argument seems not to work properly.
Any help to resolve this issue would be greatly appreciated, thank you !

C++ Code

// Main.cxx
import std;
int main(void)
{
    std::cout << "Hello world" << std::endl;
    return 0;
}

Version of emscripten/emsdk:

$ em++ -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.50 (047b82506d6b471873300a5e4d1e690420b582d0)
clang version 18.0.0 (https://github.com/llvm/llvm-project 14028ec0a62210d68a4dd7a046ac79c8c3b7727e)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/ptah/Tools/emsdk/upstream/bin

Failing command line in full:

$ em++ -std=c++23 -stdlib=libc++ -fmodules -fbuiltin-module-map Main.cxx
Main.cxx:1:9: fatal error: module 'std' not found
    1 | import  std;  //everything in std namespace is in here
      | ~~~~~~~~^~~
1 error generated.
em++: error: '/home/ptah/Tools/emsdk/upstream/bin/clang++ -target wasm32-unknown-emscripten -fignore-exceptions -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --sysroot=/home/ptah/Tools/emsdk/upstream/emscripten/cache/sysroot -DEMSCRIPTEN -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -std=c++23 -stdlib=libc++ -fmodules -fbuiltin-module-map Main.cxx -c -o /tmp/emscripten_temp__bnx4x9u/Main_0.o' failed (returned 1)

** Working command line in full using clang:**

$ clang++ -std=c++23 -stdlib=libc++ -fmodules -fbuiltin-module-map Main.cxx

Full link command and output with -v appended:

$ em++ -v -std=c++23 -stdlib=libc++ -fmodules -fbuiltin-module-map Main.cxx 
 "/home/ptah/Tools/emsdk/upstream/bin/clang++" -target wasm32-unknown-emscripten -fignore-exceptions -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --sysroot=/home/ptah/Tools/emsdk/upstream/emscripten/cache/sysroot -DEMSCRIPTEN -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -v -std=c++23 -stdlib=libc++ -fmodules -fbuiltin-module-map Main.cxx -c -o /tmp/emscripten_temp_5ll0y5z5/Main_0.o
clang version 18.0.0 (https://github.com/llvm/llvm-project 14028ec0a62210d68a4dd7a046ac79c8c3b7727e)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/ptah/Tools/emsdk/upstream/bin
 (in-process)
 "/home/ptah/Tools/emsdk/upstream/bin/clang-18" -cc1 -triple wasm32-unknown-emscripten -emit-obj -mrelax-all -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name Main.cxx -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/home/ptah/Projects/LexoMusic/Native/Test -v -fcoverage-compilation-dir=/home/ptah/Projects/LexoMusic/Native/Test -resource-dir /home/ptah/Tools/emsdk/upstream/lib/clang/18 -D EMSCRIPTEN -isysroot /home/ptah/Tools/emsdk/upstream/emscripten/cache/sysroot -internal-isystem /home/ptah/Tools/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten/c++/v1 -internal-isystem /home/ptah/Tools/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1 -internal-isystem /home/ptah/Tools/emsdk/upstream/lib/clang/18/include -internal-isystem /home/ptah/Tools/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten -internal-isystem /home/ptah/Tools/emsdk/upstream/emscripten/cache/sysroot/include -std=c++23 -fdeprecated-macro -ferror-limit 19 -fvisibility=default -fgnuc-version=4.2.1 -fmodules -fimplicit-module-maps -fmodules-cache-path=/home/ptah/.cache/clang/ModuleCache -fmodule-map-file=/home/ptah/Tools/emsdk/upstream/lib/clang/18/include/module.modulemap -fmodules-validate-system-headers -fcxx-exceptions -fignore-exceptions -fexceptions -fcolor-diagnostics -iwithsysroot/include/fakesdl -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o /tmp/emscripten_temp_5ll0y5z5/Main_0.o -x c++ Main.cxx
clang -cc1 version 18.0.0 based upon LLVM 18.0.0git default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/home/ptah/Tools/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten/c++/v1"
ignoring nonexistent directory "/home/ptah/Tools/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten"
#include "..." search starts here:
#include <...> search starts here:
 /home/ptah/Tools/emsdk/upstream/emscripten/cache/sysroot/include/fakesdl
 /home/ptah/Tools/emsdk/upstream/emscripten/cache/sysroot/include/compat
 /home/ptah/Tools/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1
 /home/ptah/Tools/emsdk/upstream/lib/clang/18/include
 /home/ptah/Tools/emsdk/upstream/emscripten/cache/sysroot/include
End of search list.
Main.cxx:1:9: fatal error: module 'std' not found
    1 | import  std;  //everything in std namespace is in here
      | ~~~~~~~~^~~
1 error generated.
em++: error: '/home/ptah/Tools/emsdk/upstream/bin/clang++ -target wasm32-unknown-emscripten -fignore-exceptions -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --sysroot=/home/ptah/Tools/emsdk/upstream/emscripten/cache/sysroot -DEMSCRIPTEN -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -v -std=c++23 -stdlib=libc++ -fmodules -fbuiltin-module-map Main.cxx -c -o /tmp/emscripten_temp_5ll0y5z5/Main_0.o' failed (returned 1)

Activity

  1. kripken commented on Jan 25, 2024

    @kripken
    Member

    The std header is pretty recent in LLVM (less than a year) and I am guessing has not been in the release of libc++ used in Emscripten yet.

  2. sbc100 commented on Jan 25, 2024

    @sbc100
    Collaborator

    LLVM 18 branched a couple of days ago so perhaps once it gets released and we update libc++ this will start working

  3. added a commit that references this issue on Jan 26, 2024
    e91418d
  4. added a commit that references this issue on Jan 3, 2025
    7c8167b
  5. self-assigned this
    on Jan 30, 2025
  6. LeenHawk commented on Feb 12, 2025

    @LeenHawk

    Just checking if there's any progress here 🙂 We're excited to see how this evolves!

  7. sbc100 commented on Feb 12, 2025

    @sbc100
    Collaborator

    There has been a lot of progress on supported C++ modules in general. See #23542 and #21987.

    However using libc++ itself via the std module seems like something that is still very experimental even upstream.
    See https://libcxx.llvm.org/Modules.html. It seems like this support still requires some special cmake setup.

    In other words, if you want to use C++ modules yourself you should be good to go. If you want to use the std module it seems like there is still work to be done, which is why I'm leaving this bug open for now.

  8. sxstd001 commented on Feb 12, 2025

    @sxstd001
  9. sbc100 commented on Feb 12, 2025

    @sbc100
    Collaborator

    See the tests added in #23542. I believe -fmodules-ts is deprecated and you can use -fmodules.

  10. sbc100 commented on Feb 12, 2025

    @sbc100
    Collaborator

    See the tests added in #23542. I believe -fmodules-ts is deprecated and you can use -fmodules.

    BTW, this is just how upstream clang behaves and not related to emcc per-say.

  11. sxstd001 commented on Feb 18, 2025

    @sxstd001

    This is too important.
    clang19:
    set(CMAKE_CXX_EXTENSIONS 0)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fmodule-file=std=/root/std.pcm"
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fmodules -fbuiltin-module-map")

    emsripten not supported;

  12. sbc100 commented on Feb 18, 2025

    @sbc100
    Collaborator

    @sxstd001 using the std module is not yet supported by emscripten. You can use you down C++20 though.

  13. sxstd001 commented on Feb 28, 2025

    @sxstd001

    When will it be possible to support it?

  14. sbc100 commented on Feb 28, 2025

    @sbc100
    Collaborator

    From the investigation I did it does not look simple. Somebody with a good understanding the current state of libc++ might be able to get it working.

  15. tiantianaixuexi commented on Jul 29, 2025

    @tiantianaixuexi

    Hello? in latest version import std Is it ready to use?

    base64.cppm:

    module;
    
    export module shine.base64;
    
    import std;
    
    #ifdef SHINE_API
    #define SHINE_API __declspec(dllexport)
    #else
    #define SHINE_API
    #endif
    
    export namespace shine::base64
    { 
        SHINE_API std::string base64_encode(const std::vector<unsigned char>& data);
        SHINE_API std::string base64_encode(std::string_view data);
        SHINE_API std::vector<unsigned char> base64_decode(std::string_view encoded_string);
    }
    
    
    em++ -std=c++23 -stdlib=libc++ -O2 -fmodules -s WASM=1 -s USE_WEBGL2=1  .\plugins\base64\base64.cppm 
    .\plugins\base64\base64.cpp -o  example.html
    
    

    error:

    .\plugins\base64\base64.cppm:6:1: error: import of module 'std' imported non C++20 importable modules
    6 | import std;
    | ^
    .\plugins\base64\base64.cppm:16:20: error: declaration of 'string' must be imported from module 'std.string.fwd' before it is required
    16 | SHINE_API std::string base64_encode(const std::vector& data);
    | ^
    F:\c++\emsdk\upstream\emscripten\cache\sysroot/include/c++/v1__fwd\string.h:45:7: note: declaration here is not visible
    45 | using string = basic_string;
    | ^
    .\plugins\base64\base64.cppm:16:52: error: declaration of 'vector' must be imported from module 'std.vector.vector' before it is required
    16 | SHINE_API std::string base64_encode(const std::vector& data);
    | ^
    F:\c++\emsdk\upstream\emscripten\cache\sysroot/include/c++/v1__vector\vector.h:86:28: note: declaration here is not visible
    86 | class _LIBCPP_TEMPLATE_VIS vector {
    | ^
    .\plugins\base64\base64.cppm:17:46: error: declaration of 'string_view' must be imported from module 'std.string_view.fwd' before it is required
    17 | SHINE_API std::string base64_encode(std::string_view data);
    | ^
    F:\c++\emsdk\upstream\emscripten\cache\sysroot/include/c++/v1__fwd\string_view.h:25:33: note: declaration here is not visible
    25 | typedef basic_string_view string_view;
    |

  16. sbc100 commented on Jul 29, 2025

    @sbc100
    Collaborator

    No, this bug is still open because import std is not yet implemented or ready to use.

  17. j4niwzis commented on Sep 23, 2025

    @j4niwzis
    Contributor

    I managed to get import std to work, but I had to manually copy files from another libcxx build and modify the CMake source code (maybe it's possible without this, but I haven't succeeded yet). I don't know how to do this correctly for emscripten to submit a pull request.

  18. Diyou commented on Sep 23, 2025

    @Diyou
    Contributor

    Afaik you can point to the libc++.modules.json location with a cmake variable like this person did here
    Don't know if this works with em++/emcc though.

  19. Diyou commented on Sep 26, 2025

    @Diyou
    Contributor
    ❯ em++ -print-file-name=libc++.modules.json -resource-dir=../lib
    libc++.modules.json
    ❯ ../bin/clang++ -print-file-name=libc++.modules.json -resource-dir=../lib
    ../lib/libc++.modules.json

    These arguments need to work with em++ to be consistent with upstream behavior. We could then supply files like libc++.modules.json std.cppm and such on our own at least.

  20. sbc100 commented on Sep 26, 2025

    @sbc100
    Collaborator

    Upstream clang does not seem to have that behaviour for me:

    $ clang++ -print-file-name=libc++.modules.json -resource-dir=../lib
    libc++.modules.json
    $ ../llvm-build/bin/clang++ -print-file-name=libc++.modules.json -resource-dir=../lib
    libc++.modules.json
    

    Here you can see my system clang (v19.1.7) and also a build of clang from tip-of-tree today. They both behave like emscripten does WRT -print-file-name=libc++.modules.json and -resource-dir

  21. Diyou commented on Sep 26, 2025

    @Diyou
    Contributor

    On arch I had to install libc++ additionally to clang for the relevant files incl. libc++.modules.json:
    If -resource-dir=../lib doesn't containt the libc++.modules.json it falls back to system paths or to what you're seeing when none can be found.

    These are run with the emsdk provided clang from the emscripten directory:

    ❯ /mnt/e/Workspace/SDLTemplate/.cache/emsdk/upstream/bin/clang++ -print-file-name=libc++.modules.json | xargs realpath
    /usr/lib/libc++.modules.json
    ❯ /mnt/e/Workspace/SDLTemplate/.cache/emsdk/upstream/bin/clang++ -print-file-name=libc++.modules.json -resource-dir=../lib | xargs realpath
    /mnt/e/Workspace/SDLTemplate/.cache/emsdk/upstream/lib/libc++.modules.json

    ^--> This file is installed via emsdk I assume but should eventually be placed together with the other relevant files near the libc++ headers in my opinion. At this moment the files and paths in that file don't exist.

    Related issue: Homebrew/homebrew-core#226050

  22. sbc100 commented on Sep 26, 2025

    @sbc100
    Collaborator

    Yes, I imagine fixing this issue will involve somehow creating/installing emsdk/upstream/lib/libc++.modules.json

  23. Diyou commented on Sep 26, 2025

    @Diyou
    Contributor

    Perhaps replicating upstream behavior to fallback to system files isn't even desirable for em++ in this case?

  24. mccakit commented on Sep 29, 2025

    @mccakit

    Yes, I imagine fixing this issue will involve somehow creating/installing emsdk/upstream/lib/libc++.modules.json

    Hmm, copying that and editing it to point to a valid std.cppm and inc files doesn't solve the issue. I tried looking at your previous closed pr, and trying to replicate it, but honestly got no results.

    Please give priority to this issue, it is quite annoying libraries cannot move to modules because of lack of platform support, currently with tweaks modules can be used on linux, mac, windows, android and ios with libc++. Emscripten is one of the last platforms remaining to not support this.

  25. sbc100 commented on Sep 29, 2025

    @sbc100
    Collaborator

    According to https://libcxx.llvm.org/Modules.html, module support is "still in an early development state and not considered stable nor complete", so it seems like even if we got it working there would likely teething issues because its not ready yet.

    I'm afraid I'm personally not going to have time to focus on this, but if anyone can figure out how to get a working modules.json then patches would be most welcome.

  26. Diyou commented on Sep 29, 2025

    @Diyou
    Contributor

    I got a successful build without modifying emcc.py or any patches:

    Setup:

    • To make em++ -print-file-name=libc++.modules.json work libc++.modules.json needs to be placed in emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/
    • Copy Clang-CXX-CXXImportStd.cmake into emsdk/upstream/emscripten/cmake/Modules/Compiler/
    • Copy std.cppm std.compat.cppm and the 2 folders with the includes std and std.compat into emsdk/upstream/emscripten/cache/sysroot/lib/share/libc++/v1/ from the llvm-build (I just copied from the system directory)

    Config:

    • Adding something likes this for c++23 to the emscripten toolchain file:
    include("${EMSCRIPTEN_ROOT}/cmake/Modules/Compiler/Clang-CXX-CXXImportStd.cmake")
    set(CMAKE_CXX_STANDARD_LIBRARY libc++)
    _cmake_cxx_import_std(23 eval_import_std)
    cmake_language(EVAL CODE "${eval_import_std}")

    ^-- There is probably a better solution but we can work on this once the setup is in place.

  27. mccakit commented on Sep 30, 2025

    @mccakit

    This indeed works, however you have to edit std.cppm and 4.inc files, for C++26 import std 2 more edits are required namely:

    • type_traits.inc
    • ranges.inc
    • numeric.inc
    • algorithm.inc

    for C++26

    • memory.inc
    • mdspan.inc

    So, in summary for the upcoming PR

    • std.cppm and std folder must be copied from llvm installation, or emscripten should be patched to work with the existing std.cppm file that way we can jsut create libc++.modules.json and then just point to llvm root inside, for example:
    {
      "version": 1,
      "revision": 1,
      "modules": [
        {
          "logical-name": "std",
          "source-path": "/home/mccakit/dev/llvm/share/libc++/v1/std.cppm",
          "is-std-library": true,
          "local-arguments": {
            "system-include-directories": [
              "/home/mccakit/dev/llvm/share/libc++/v1"
            ]
          }
        },
        {
          "logical-name": "std.compat",
          "source-path": "/home/mccakit/dev/llvm/share/libc++/v1/std.compat.cppm",
          "is-std-library": true,
          "local-arguments": {
            "system-include-directories": [
              "/home/mccakit/dev/llvm/share/libc++/v1"
            ]
          }
        }
      ]
    }
    

    std edits.tar.gz

    I edited my .inc files with macros

    and then

    include("/home/mccakit/dev/cmake/share/cmake-4.1/Modules/Compiler/Clang-CXX-CXXImportStd.cmake")
    set(CMAKE_CXX_STANDARD_LIBRARY libc++)
    _cmake_cxx_import_std(26 eval_import_std)
    cmake_language(EVAL CODE "${eval_import_std}")
    include(/home/mccakit/dev/emscripten/cmake/Modules/Platform/Emscripten.cmake)
    add_compile_options(-w -sALLOW_MEMORY_GROWTH -sINITIAL_MEMORY=256MB -flto)
    add_link_options(-w -sALLOW_MEMORY_GROWTH -sINITIAL_MEMORY=256MB -flto)
    
  28. added a commit that references this issue on Jun 15, 2026
    d913170
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions