emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.57k stars 3.27k forks source link

Does emscripten support c++17 special math functions? #20693

Open adapt-L opened 9 months ago

adapt-L commented 9 months ago

Version of emscripten/emsdk:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.46 (19607820c447a13fd8d0b7680c56148427d6e1b8)
clang version 18.0.0 (https://github.com/llvm/llvm-project 75501f53624de92aafce2f1da698b249a7293dc7)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/adam/emsdk/upstream/bin

Here's an example c++ program that uses special math functions (smf.cpp):

#define __STDCPP_WANT_MATH_SPEC_FUNCS__ 1
#include <iostream>
#include <cmath>
int main(){
    std::cout << std::cyl_bessel_j(1.0,1.0) << std::endl;
    return 0;
}

Compiles natively with g++ smf.cpp or clang -lm -lstdc++ smf.cpp. When I try to compile with em++ -v smf.cpp, I get the following error:

 "~/emsdk/upstream/bin/clang" --version
 "~/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 -DEMSCRIPTEN --sysroot=~/emsdk/upstream/emscripten/cache/sysroot -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -v smf.cpp -c -o /tmp/emscripten_temp_g_j458yj/smf_0.o
clang version 18.0.0 (https://github.com/llvm/llvm-project 75501f53624de92aafce2f1da698b249a7293dc7)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: ~/emsdk/upstream/bin
 (in-process)
 "~/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 smf.cpp -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=~/Downloads -v -fcoverage-compilation-dir=~/Downloads -resource-dir ~/emsdk/upstream/lib/clang/18 -D EMSCRIPTEN -isysroot ~/emsdk/upstream/emscripten/cache/sysroot -internal-isystem ~/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten/c++/v1 -internal-isystem ~/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1 -internal-isystem ~/emsdk/upstream/lib/clang/18/include -internal-isystem ~/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten -internal-isystem ~/emsdk/upstream/emscripten/cache/sysroot/include -fdeprecated-macro -ferror-limit 19 -fvisibility=default -fgnuc-version=4.2.1 -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_g_j458yj/smf_0.o -x c++ smf.cpp
clang -cc1 version 18.0.0 based upon LLVM 18.0.0git default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "~/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten/c++/v1"
ignoring nonexistent directory "~/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten"
#include "..." search starts here:
#include <...> search starts here:
 ~/emsdk/upstream/emscripten/cache/sysroot/include/fakesdl
 ~/emsdk/upstream/emscripten/cache/sysroot/include/compat
 ~/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1
 ~/emsdk/upstream/lib/clang/18/include
 ~/emsdk/upstream/emscripten/cache/sysroot/include
End of search list.
smf.cpp:5:20: error: no member named 'cyl_bessel_j' in namespace 'std'
    5 |         std::cout << std::cyl_bessel_j(1.0,1.0) << std::endl;
      |                      ~~~~~^
1 error generated.
em++: error: '~/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 -DEMSCRIPTEN --sysroot=~/emsdk/upstream/emscripten/cache/sysroot -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -v smf.cpp -c -o /tmp/emscripten_temp_g_j458yj/smf_0.o' failed (returned 1)
sbc100 commented 9 months ago

I don't see cyl_bessel_j defined in either musl (which emscripten uses for libc) to libc++ (the llvm C++ library which emscripten uses it's C++ stdlib).

I'm guessing that in both your working examples of gcc and clang that definition was coming from libstdc++ (the GNU++ library), which is not supported by emscripten.

So I think the answer is no, emscripten doesn't not support this function, because that function not part of the libraries on which emscripten is built.

adapt-L commented 9 months ago

Thanks. Is it possible to use glibc or libstc++ with emscripten?

sbc100 commented 9 months ago

Thanks. Is it possible to use glibc or libstc++ with emscripten?

I don't know of anyone who has tried that. I imagine it would be significant amount of work.