I have a CMake project (C++) that I want to make available in JavaScript through WebAssembly. To configure it, I use emcmake cmake
and to build it emmake make
. I can compile parts successfully, when I do it manually:
emcc --bind test.cpp
But I want to profit from the advantages of emmake
. I need the parameter --bind
for emcc
. emmake
does not add it by default, which results in an error:
error: undefined symbol: _embind_register_function (referenced by top-level compiled C/C++ code)
So, how do I add it, when building with emmake make
? Can I pass it to emmake
? Or can I add something to my CMakeLists.txt
?
MCRE:
CMakeLists.txt
:
cmake_minimum_required(VERSION 2.8)
project(MyTest)
add_executable(mytest test.cpp)
test.cpp
:
#include "emscripten/bind.h"
using namespace emscripten;
std::string getText()
{
return "Hello there from C++!";
}
EMSCRIPTEN_BINDINGS(my_module) {
function("getText", &getText);
}
It turned out, that you can pass the emcc
options from within the CMakeLists.txt
file, by using set_target_properties(...)
:
CMakeLists.txt
:
cmake_minimum_required(VERSION 2.8)
project(MyTest)
add_executable(mytest test.cpp)
set_target_properties(mytest PROPERTIES LINK_FLAGS "--bind")
This approach works for nearly all paramters, except the -o
parameter to control the output files:
set(EXECUTABLE_OUTPUT_PATH subdir/for/emscripten)
before executing add_executable(...)
set(CMAKE_EXECUTABLE_SUFFIX ".mjs")
according to your needs before executing add_executable(...)