Reputation: 163
I'm trying to use WebAssembly and libc++ for browser app. I've successfully added libc++ headers to my C++ code and compiled it. Without linking libc++ there is a need to import some functions to wasm from JS (memcpy, operator new...). But if i link clang libc++ to my code, there appears an error "wasm-ld unknown file type new.cpp.o" on linking time. OK, this is quite clear.
If i link wasi-libc and use its headers, everything works fine. But i know that if i don't want to run my code anywhere, but only on browser, i should use wasm instead of wasi. Is it true? If yes, what is the proper way to link libc++ to my code?
Upvotes: 2
Views: 778
Reputation: 33
I'm trying to use WebAssembly and libc++ for browser app.
Emscripten includes a web-compatible implementation of libc++, as indicated in their FAQ:
How do I link against system libraries like SDL, boost, etc.?
System libraries that are included with emscripten are automatically linked when you compile (just the necessary parts). This includes libc, libc++ (C++ standard library) and SDL.
if i link clang libc++ to my code
Clang's libc++ is most likely compiled for your host architecture (i.e. natively for your development computer), and not wasm. Therefore, it won't be able to link with your wasm code.
If i link wasi-libc and use its headers, everything works fine.
To use wasi, you need a wasi runtime. The wasi runtime will provide implementations of wasi system calls, which are imported functions analagous to POSIX system calls. Wasmer's runtime says it has support for the browser, though I haven't verified this.
But i know that if i don't want to run my code anywhere, but only on browser, i should use wasm instead of wasi. Is it true?
Wasi runs on wasm; if you use wasi, you are using wasm: your own wasm code + wasi's wasm code.
If you mean emscripten, I personally would choose emscripten. Emscripten can generate helpful js glue code; Wasi on the other hand, interfaces with it's environment (the browser in your case) through things like stdin, stdout, files, etc.
what is the proper way to link libc++ to my code?
Both wasi and emscripten offer wasm implementations of libc++. I am unaware of any others.
Upvotes: 0