1

I have a basic C++ GLUT program and I am trying to run Javascript code from inside C++ functions. I found Emscripten and done lots of testing, but I can't figure out how to actually use it in my projects. I added the emscripten folder to my compiler's search directories and was able to #include , but I keep getting 'Undefined reference to emscripten_run_script.' I know this is a problem with not linking the libraries (-1SDL, -1glew32, etc.). I checked examples and tutorials from the Emscripten website, but I don't need code, I need example projects or just the name and location of the Emscripten library. How do I link Emscripten so that I can call Emscripten functions from my program, or is it even possible?

EDIT:

Since Emscripten is a Javascript compiler, is there a way I can (instead of linking the library to my project) have Emscripten stand in as the compiler for part of the code, then switch back to g++?

  • 1
    Are you sure Emscripten is what you need? From a quick look at first Google hits, it seems to be a C++->Javascript compiler, not a Javascript interpreter. – Angew is no longer proud of SO Mar 05 '15 at 13:34
  • On their website they have a section called Calling JavaScript from C/C++. It has examples of cpp files including emscripten.h, which i was able to do, and called functions like emscripten_run_script. – ArcaneEnforcer Mar 05 '15 at 13:39
  • Well, compiling your C++ program to javascript would technically let you execute javascript in your "c++" functions...sort of :p – melak47 Mar 05 '15 at 13:39
  • I'm not able to compile my program to javascript because i am using glut and opengl functions. – ArcaneEnforcer Mar 05 '15 at 13:41
  • Although I did try to use the emscripten command prompt to convert my c++ code to js :D – ArcaneEnforcer Mar 05 '15 at 13:46

1 Answers1

0

Here's an example project that works when you add calls to emscripten_run_script. Download Tim Hutton's OpenGL sample program in C++ and add this code as the first line of the main() function:

emscripten_run_script("alert('hi')");

It works perfectly. I have just tested it and it displays an alert as designed. I used the latest Emscripten SDK on Windows and used this command to compile main.cpp:

emcc main.cpp -s WASM=1 -s USE_SDL=2 -O3 -o index.js

I also tried the inline JavaScript method using EM_JS. You can read about all these things here.

I have tested this both when compiling to WebAssembly (-s WASM=1) and to JavaScript (-s WASM=0). Both cases work perfectly and have identical behaviour.

Graham Asher
  • 1,648
  • 1
  • 24
  • 34