floooh / sokol

minimal cross-platform standalone C headers
https://floooh.github.io/sokol-html5
zlib License
6.82k stars 475 forks source link

Building Sokol with Emscripten and WebGL2 #837

Closed geekynils closed 1 year ago

geekynils commented 1 year ago

This is slightly off-topic since it is not a sokol problem. Unfortunately I do not know where to ask and could not find the answer in the CMakeLists.txt files from sokol-samples.

I upgraded my app to WebGL 2 since WebGL 1 is no longer supported by sokol. However I now run into linker problems on my Mac:

em++: warning: disabling closure because debug info was requested [-Wemcc]
wasm-ld: error: libsokol.a(sokol_impl.c.o): undefined symbol: glClearBufferfv
wasm-ld: error: libsokol.a(sokol_impl.c.o): undefined symbol: glClearBufferfi
wasm-ld: error: libsokol.a(sokol_impl.c.o): undefined symbol: glClearBufferfv
wasm-ld: error: libsokol.a(sokol_impl.c.o): undefined symbol: glClearBufferiv
wasm-ld: error: libsokol.a(sokol_impl.c.o): undefined symbol: glReadBuffer
wasm-ld: error: libsokol.a(sokol_impl.c.o): undefined symbol: glBlitFramebuffer
wasm-ld: error: libsokol.a(sokol_impl.c.o): undefined symbol: glInvalidateFramebuffer
wasm-ld: error: libsokol.a(sokol_impl.c.o): undefined symbol: glTexSubImage3D
wasm-ld: error: libsokol.a(sokol_impl.c.o): undefined symbol: glGetStringi
wasm-ld: error: libsokol.a(sokol_impl.c.o): undefined symbol: glRenderbufferStorageMultisample
wasm-ld: error: libsokol.a(sokol_impl.c.o): undefined symbol: glCompressedTexImage3D
wasm-ld: error: libsokol.a(sokol_impl.c.o): undefined symbol: glTexImage3D

I tried two things to fix this:

Using the OpenGL package in the cmake file with find_package(OpenGL) and then adding

target_link_libraries(my_app sokol imgui ${OPENGL_LIBRARIES})

which ended with the same error.

Adding target_link_libraries(my_app PUBLIC "-framework OpenGL") did not work either, I kind of expected this because the emscripten compiler is not an apple tool.

Compiling the sokol samples for the web (webgl2-emsc-make-release) was not an issue, I guess fips somehow takes care of OpenGL dependencies. I just could not figure out what it does.

floooh commented 1 year ago

Hi, try passing -s USE_WEBGL2=1 to the linker step (it's also mentioned in the changelog entry from 30-Apr): https://github.com/floooh/sokol/blob/master/CHANGELOG.md

Unfortunately Emscripten still defaults to WebGL1, but requires the above linker option to propely link with WebGL2.

PS: I actually just fixed the cimgui-sokol-starterkit project, see here:

https://github.com/floooh/cimgui-sokol-starterkit/blob/62271de00bd7851de548a30216e5c40bfbc00f5e/CMakeLists.txt#L57-L66

geekynils commented 1 year ago

Ah I overlooked that and thought it is a problem with OpenGL on my Mac. Thank you so much!