glfw-rs
glfw-rs copied to clipboard
Compile error with wasm
I am trying to build the minimal example from the readme to wasm. By default it tries to build glfw using cmake, and that doesn't seem to work properly in terms of cross-compiling:
$ cargo build --target wasm32-unknown-unknown
[..]
The C compiler
"/usr/bin/clang"
is not able to compile a simple test program.
[..]
Linking C executable cmTC_77bbe
/usr/bin/clang -ffunction-sections -fdata-sections -fPIC --target=wasm32-unknown-unknown CMakeFiles/cmTC_77bbe.dir/testCCompiler.c.o -o cmTC_77bbe
wasm-ld: error: cannot open crt1.o: No such file or directory
wasm-ld: error: unable to find library -lc
wasm-ld: error: unable to find library -lgcc
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Next I tried with default-features = false in Cargo.toml, and I get this error:
$ cargo build --target wasm32-unknown-unknown
Compiling glfw v0.43.0 (https://github.com/bjz/glfw-rs.git#5298cfc6)
error[E0308]: mismatched types
--> /home/azakai/.cargo/git/checkouts/glfw-rs-9212cdc098378853/5298cfc/src/lib.rs:2903:50
|
2903 | fn raw_window_handle<C: Context>(context: &C) -> RawWindowHandle {
| ----------------- ^^^^^^^^^^^^^^^ expected enum `RawWindowHandle`, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression
For more information about this error, try `rustc --explain E0308`.
error: could not compile `glfw` due to previous error
As far as I know GLFW is an OpenGL library, but you can only use WebGL with WebAssembly. So I'm not sure you can do that.
For WebGL wasm example, please see this: https://rustwasm.github.io/wasm-bindgen/examples/webgl.html
I don't think you can compile rust + C bindings to wasm. there's an ABI issue and fixing it breaks backwards compatibility apparently. more info https://github.com/rustwasm/wasm-bindgen/pull/2209
glfw + wasm-emscripten works with a small patch.
deployed version -> https://coderedart.github.io/egui_glow_glfw_emscripten/ code -> https://github.com/coderedart/egui_glow_glfw_emscripten/
the patch to make glfw-rs compile on emscripten target -> https://github.com/PistonDevelopers/glfw-rs/pull/531
you can then compile it to emscripten by using default-features = false to avoid glfw-sys. instead glfw symbols are provided by emscripten itself.
emscripten is using glfw 3.2, so we will obviously miss some methods like content scale or window hint string from glfw 3.3, but atleast the rest work fine.
once the patch is merged, i think this issue can be closed.