Repository navigation
Closed
Description
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)
Reacted by Duncan Holm and Damian Höster
Metadata
Metadata
Assignees
Labels
No labels
Activity
kripken commentedon Jan 25, 2024
The
stdheader 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.sbc100 commentedon Jan 25, 2024
LLVM 18 branched a couple of days ago so perhaps once it gets released and we update libc++ this will start working
[WIP] Add `modules` directory for libcxx
[WIP] Add `modules` directory for libcxx
LeenHawk commentedon Feb 12, 2025
Just checking if there's any progress here 🙂 We're excited to see how this evolves!
sbc100 commentedon Feb 12, 2025
There has been a lot of progress on supported C++ modules in general. See #23542 and #21987.
However using libc++ itself via the
stdmodule 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
stdmodule it seems like there is still work to be done, which is why I'm leaving this bug open for now.sxstd001 commentedon Feb 12, 2025
sbc100 commentedon Feb 12, 2025
See the tests added in #23542. I believe
-fmodules-tsis deprecated and you can use-fmodules.sbc100 commentedon Feb 12, 2025
BTW, this is just how upstream clang behaves and not related to emcc per-say.
sxstd001 commentedon Feb 18, 2025
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;
sbc100 commentedon Feb 18, 2025
@sxstd001 using the
stdmodule is not yet supported by emscripten. You can use you down C++20 though.sxstd001 commentedon Feb 28, 2025
When will it be possible to support it?
sbc100 commentedon Feb 28, 2025
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.
tiantianaixuexi commentedon Jul 29, 2025
Hello? in latest version import std Is it ready to use?
base64.cppm:
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;
|
sbc100 commentedon Jul 29, 2025
No, this bug is still open because
import stdis not yet implemented or ready to use.j4niwzis commentedon Sep 23, 2025
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.
Diyou commentedon Sep 23, 2025
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.
Diyou commentedon Sep 26, 2025
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.
sbc100 commentedon Sep 26, 2025
Upstream clang does not seem to have that behaviour for me:
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.jsonand-resource-dirDiyou commentedon Sep 26, 2025
On arch I had to install
libc++additionally toclangfor the relevant files incl.libc++.modules.json:If
-resource-dir=../libdoesn't containt thelibc++.modules.jsonit 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:
^--> 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
sbc100 commentedon Sep 26, 2025
Yes, I imagine fixing this issue will involve somehow creating/installing
emsdk/upstream/lib/libc++.modules.jsonDiyou commentedon Sep 26, 2025
Perhaps replicating upstream behavior to fallback to system files isn't even desirable for em++ in this case?
mccakit commentedon Sep 29, 2025
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.
sbc100 commentedon Sep 29, 2025
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.jsonthen patches would be most welcome.Diyou commentedon Sep 29, 2025
I got a successful build without modifying emcc.py or any patches:
Setup:
em++ -print-file-name=libc++.modules.jsonworklibc++.modules.jsonneeds to be placed inemsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/emsdk/upstream/emscripten/cmake/Modules/Compiler/std.cppmstd.compat.cppmand the 2 folders with the includesstdandstd.compatintoemsdk/upstream/emscripten/cache/sysroot/lib/share/libc++/v1/from the llvm-build (I just copied from the system directory)Config:
^-- There is probably a better solution but we can work on this once the setup is in place.
mccakit commentedon Sep 30, 2025
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:
for C++26
So, in summary for the upcoming PR
std edits.tar.gz
I edited my .inc files with macros
and then
Add support for C++23 std modules (#27065)