How do I pass 'emcc' options through 'emcmake cmake/emmake make'?

5k Views Asked by At

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);
}
1

There are 1 best solutions below

0
On BEST ANSWER

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:

  • If you want to change the name of the output, change the name of the target.
  • If you want to change the directory of the output, change the executable output path with set(EXECUTABLE_OUTPUT_PATH subdir/for/emscripten) before executing add_executable(...)
  • If you want to change the file type of the output, change the executable suffix with set(CMAKE_EXECUTABLE_SUFFIX ".mjs") according to your needs before executing add_executable(...)