How to link multiple wasm files together in emscripten? (Not just C)

Viewed 672

I am trying to link a .c file and a .wat file together using emscripten, so that I can call native webassembly functions from c. I've tried several things, but none seem to work. For instance if I try something like this:

watfile.wat

(module
  (func $addwat (param $p1 i32) (param $p2 i32) (result i32)
    local.get $p1     
    local.get $p2 
    i32.add 
  )
  (export "addwat" (func $addwat))
)

cfile.c

#include <emscripten.h>

int addwat( int a, int b );
int add2( int a, int b )
{
    return a+b;
}
int add2cwat( int a, int b )
{
    return addwat( a, b );
}
wat2wasm -r watfile.wat
emcc -o cfile.wasm cfile.c watfile.wasm -s EXPORTED_FUNCTIONS='["_add2","_add2cwat"]' -s EXPORTED_RUNTIME_METHODS='["ccall","cwrap"]' -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s ASYNCIFY=1 -s EXPORT_ALL=1 -s SIDE_MODULE=1
emcc -o c-test.wasm cfile.wasm watfile.wasm  -s EXPORTED_FUNCTIONS='["_add2wat","_add2","_add2cwat"]' -s ASYNCIFY=1 -s EXPORT_ALL=1 --no-entry -s STANDALONE_WASM=1
emcc: error: undefined exported function: "_add2" [-Wundefined] [-Werror]

I can force everything to link, but examining the linked output, the symbols get lost. What is the proper way to link to gether multiple wasm files? Or better yet - how it is possible to link to a wasm file from an emscripten'd C file?

EDIT: Just as a note, you cannot seem to use wasm-ld to link emscripten outputted WASM together without sacrificing everything emscripten buys you. I.e. it requires relocatable files -s RELOCATABLE=1, but you can only do that if you are just compiling a side module -s SIDE_MODULE=1 it seems, which means you lose _start

0 Answers
Related