Search code examples
javascriptc++emscriptenammo.js

How to link .bc files compiled from cpp files using emcc, with bullet libraries?


As per the documentation of ammo.js, it says:

The most straightforward thing is if you want to write your code in C++, and run that on the web. If so, then compile your code into LLVM, link it with bullet, and compile that to JavaScript using emscripten.(The easiest way to link it is to add your .bc file to the llvm-link command in make.py.)

So how can I compile the following code written in C++ into LLVM bitcode, link it and run it on web? BulletHelloWorld example

How can I link it in make.py? Is it necessary to always use the ammo.idl file even if I want to compile a specific program and not want to expose the entire bullet library to JavaScript? Link to make.py


Solution

  • So lets start with the basics. Incase you donot know about make and cmake, study it before proceeding.

    First you need to build the Bullet Library from source to use it on the web. From what I can see, you need to pass in flags to build it independent of python. Study these flags and see what is required by you.

    The Bullet Library is using cmake to generate the build files - so first get a makefile out of cmake and then you can “emcc make” the generated makefile.

    The output of this step ie a .bc file, is to be “linked” to the output of the next step.

    Now the example.cpp that you want to compile depends on some headers of the Bullet Library. So while compiling your main.cpp file you will need to pass the em++ binary the path to these headers. This once compiled should generate your main.bc

    Now you need to call em++ again, but with the main.bc along with the .bc from the previous step as parameters and also provide the required output file ie js/html. In one sense we are now linking all the “.bc” files to generate js/html.

    Lookout for some missing symbol “warnings” as that could mean your code wont run.

    Btw all this is available on the official emscripten website, so incase of confusion you should refer it.