Question

I'm trying to compile a simple HelloWorld Prgramm from C++ to Javascript using emscripten. It works fine when I include a main function which call's e.g. the multi function. Here is my code (HelloWorld.cpp).

#include <stdio.h>

class HelloWorld {
    public: void sayHello() {
        printf("Hello World Klasse! %f", multi(7));
    }

    public: double multi(double x){
        return x * x;
    }
};

However if I don't include a main function the emcc compile always put's out

ERROR root: No functions to process. Make sure you prevented LLVM from eliminating them as dead (use EXPORTED_FUNCTIONS if necessary, see the FAQ)

I know about the 'EXPORTED_FUNCTIONS' option which tells what functions should be included into the compile .js file. I tried various diffrent things:

  1. Using the mangling name, as far as I understood this the name should be '_multi_d10HelloWorldd'. I also tried without classname and some other combinations.

    emcc -s HelloWorld.cpp -s EXPORTED_FUNCTIONS='["_multi_d10HelloWorldd"]'
    
  2. Using the modifier EXPORT_ALL

    emcc -s HelloWorld.cpp -s EXPORT_ALL=1
    

Whatever I do the functions won't be included in the final js file.

From what I understand from the EMCC FAQ I need to use EXPORTED_FUNCTIONS so I can later on call the desired function e.g. 'sayHello' from JS unsing the same method name. And this is exactly what I need to do later on.

Could someone please point me to a solution or any other possible option which I may have not thought of ?

Is the mangling name I thought of correct ?

Was it helpful?

Solution

Create an "extern c" block. Inside this block define the functions you want to expose to javascript. These functions should be prefixed with an underscore. Inside one of these functions you can instantiate your C++ class.

This is the same approach as one would take when writing a dynamic library, which has the advantage that you can reuse your library in a native program should you wish.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top