Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using libraries with emscripten

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?

like image 428
jmoggr Avatar asked Dec 27 '14 16:12

jmoggr


People also ask

What is Emscripten used for?

Emscripten is an LLVM/Clang-based compiler that compiles C and C++ source code to WebAssembly (or to a subset of JavaScript known as asm. js, its original compilation target before the advent of WebAssembly in 2017), primarily for execution in web browsers.

How do you build with Emscripten?

To build using Emscripten you need to replace gcc with emcc in your makefiles. This is done using emconfigure, which sets the appropriate environment variables like CXX (C++ compiler) and CC (the compiler).

Can you use C with JavaScript?

It is possible to implement a C API in JavaScript! This is the approach used in many of Emscripten's libraries, like SDL1 and OpenGL. You can use it to write your own APIs to call from C/C++.

What is Emcmake?

emcmake is a helper for cmake, setting various environment. variables so that emcc etc.


1 Answers

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

like image 101
CoffeDeveloper Avatar answered Sep 18 '22 22:09

CoffeDeveloper