Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile c++ to javascript using emscripten while including third party libraries?

Actually I'm using GSL in my c++ program. I've referred to the emscripten documentation but got nothing. This is what I see in the documentation, which makes no sense:

# For example, consider the case where a project “project” uses a library “libstuff”:
# Compile libstuff to bitcode
./emconfigure ./configure
./emmake make

# Compile project to bitcode
./emconfigure ./configure
./emmake make

# Compile the library and code together to HTML
emcc project.bc libstuff.bc -o final.html

(Isn't there something wrong with the documentation?) And it says nothing about how to use the library in javascript.

Here's the problems I have:

  1. How to build a third party library into bitcode?
  2. How to use the library in javascript?

Thanks in advance.

like image 382
SPiCa Avatar asked May 19 '15 14:05

SPiCa


People also ask

Can you run C code in JavaScript?

js can dynamically load an external C or C++ DLL file at runtime and utilize its API to perform some operations written inside it from a JavaScript program. This is basically how a Native Addon works in Node.

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).

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.

What is Embind?

Embind is used to bind C++ functions and classes to JavaScript, so that the compiled code can be used in a natural way by “normal” JavaScript. Embind also supports calling JavaScript classes from C++. Embind has support for binding most C++ constructs, including those introduced in C++11 and C++14.


1 Answers

You will need access to the C/C++ source code of the third party library and compile it using Emscripten before you can link with it in your program.

Once you have compiled the third party library with Emscripten, you can now statically link with it and use it normally in your C/C++ program, which also needs to be built with Emscripten.

If you don't have your own C/C++ program, Emscripten 1.32.2 now supports building dynamic libraries into Javascript modules that you can use on your web page.

like image 108
Deathicon Avatar answered Nov 15 '22 00:11

Deathicon