Using libraries with emscripten
Asked Answered
D

1

9

I have just started using Emscripten and would like to start using GLFW and other libraries. I am completely lost on how to build, link, and use other libraries with Emscripten.

I have tried following the instructions on the Emscripten site but have they haven't helped me any. http://kripken.github.io/emscripten-site/docs/compiling/Building-Projects.html#using-libraries

Is there any place with detailed instructions on how to use libraries with Emscripten? Or specifically GLFW?

Definitive answered 27/12, 2014 at 16:0 Comment(0)
E
16

Emscripten provide itself very few libraries. Those libraries are the minimum to get some OperativeSystem functionality on emscripten C++ code (audio, input, video)

  • libc: standard library for C
  • libc++: standard library for C++
  • SDL: SimpleDirectmediaLayer (SDL 1.X a opensource cross-platform project)
  • GLES2: OpenGL ES 2 API
  • GLFW: GLFW 2.X

For example, the standard way to include OpenGLES2 in Emscripten is:

#include <GLES2/gl2.h>

While to include GLFW:

#include <GL/glfw.h>

There's some crap in that, because if you want to use the more recent version of GLFW you just can't because Emscripten provides only 1 version of the library and you have to stick with that (unless Emscripten do a update for that and you update Emscripten).

You can compile libraries for emscripten only if that libraries can be compiled using one(or more) of the libraries listed above. (or if you know how to wrap javascript funciontalities and expose them through C interface)

Also, try to avoid templates only libraries when using Emscripten, they literally generate a lot of bloat code you could easily increase executable size by several MBs: This is a problem if you were already using Boost or UBLAS.

Since GLFW is not one of the libraries that are automatically linked, you should link it with:

-lglfw

You can find an example OpenGL project using Emscripten here:

https://github.com/QafooLabs/emscripten-opengl-example

you can inspect linker flags by opening the makefile

Epi answered 27/12, 2014 at 16:47 Comment(9)
SDL2 is also available with -s USE_SDL=2Piassava
Thank you for your response! This did help clear things up for me. I am however still having difficulty building. With a command like: em++ glfw-test.cpp -o glfw-test.html i am getting errors like: warning: unresolved symbol: glfwCreateWindow. I feel like i'm missing something really simple.Definitive
@PeterT, SDL2 now should compile under Emscripten without the necessity to use the Emscripten version. @TheRoach, you of course need to link against the GLFW library -lglfw should sufficeEpi
Thanks @DarioOO very good answer, I'm still waiting for the "glm" matrix to be added to Emscripten. Does anyone know when it will be added ? I can't find anything from researching it.Aerospace
glm can be compiled with Emscripten because is platform independent (literally add glm source code to your source tree, and provided you have correct path to includes and you defined correct GLM configuration macros that should compile).Epi
@DarioOO Thanks, sorry for my late reply, but what exactly do you mean by "GLM configuration macros" and where would it go ? can you give an example of a basic one, I checked this. gridlab-d.sourceforge.net/wiki/index.php/… but it has no examples.Aerospace
#define GLM_FORCE_PURE before including GLM in every place you use it. Hopefully create a new header file #define GLM_FORCE_PURE (newline) #include <glm/glm.hpp>.. It should work out-of-the-box. Use a new header so that you don't have to remember to define GLM_FORCE_PURE everytime you include itEpi
Since GLM is header only is there no way to create something like ammo.js with it. How would you compile it to a glm.js?Buenabuenaventura
You do not compile GLM into a JS file, instead every stuff that gets compiled into a JS file can include GLMEpi

© 2022 - 2024 — McMap. All rights reserved.