I'm new to the world of WebAssembly/Emscripten. I found this example and I would like to try it locally (mac OS). These are my files:
#include <string>
#include <emscripten.h>
#include <emscripten/val.h>
using namespace emscripten;
std::string decode_request(val window) {
val location = window["location"];
std::string search = location["search"].as<std::string>();
size_t pos = search.find('?');
if (pos != std::string::npos) {
return search.substr(pos + 1);
} else {
return "";
}
}
extern "C" {
EMSCRIPTEN_KEEPALIVE
const char* myFunction() {
val window = val::global("window");
std::string query = decode_request(window);
return query.c_str();
}
}
const wasmModule = './module.wasm';
const importObject = {};
async function initializeWasm() {
const { instance } = await WebAssembly.instantiateStreaming(fetch(wasmModule), importObject);
const result = instance.exports.myFunction();
document.getElementById('result').textContent = result;
}
initializeWasm();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebAssembly Example</title>
</head>
<body>
<h1>WebAssembly Example</h1>
<p id="result"></p>
<script src="script.js"></script>
</body>
</html>
I should have everything set up locally. When I run the command: emcc --version I can see:
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.56 (cf90417346b78455089e64eb909d71d091ecc055)
Copyright (C) 2014 the Emscripten authors (see AUTHORS.txt)
This is free and open source software under the MIT license.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
And I see the environment variables:
EMSDK=/Users/myuser/Documents/Emscripten/emsdk
EMSDK_NODE=/Users/myuser/Documents/Emscripten/emsdk/node/16.20.0_64bit/bin/node
EMSDK_PYTHON=/Users/myuser/Documents/Emscripten/emsdk/python/3.9.2_64bit/bin/python3
When I run the command:
emcc module.cpp -o module.wasm -s WASM=1
I get these errors:
module.cpp:23:16: warning: address of stack memory associated with local variable 'query' returned [-Wreturn-stack-address]
23 | return query.c_str();
| ^~~~~
1 warning generated.
wasm-ld: error: /var/folders/bn/gbn9_dvx3vq3h4k_k2wm0k4w0000gp/T/emscripten_temp_h5tvwd58/module_0.o: undefined symbol: _emval_get_property
wasm-ld: error: /var/folders/bn/gbn9_dvx3vq3h4k_k2wm0k4w0000gp/T/emscripten_temp_h5tvwd58/module_0.o: undefined symbol: _emval_get_property
wasm-ld: error: /var/folders/bn/gbn9_dvx3vq3h4k_k2wm0k4w0000gp/T/emscripten_temp_h5tvwd58/module_0.o: undefined symbol: _emval_as
wasm-ld: error: /var/folders/bn/gbn9_dvx3vq3h4k_k2wm0k4w0000gp/T/emscripten_temp_h5tvwd58/module_0.o: undefined symbol: _emval_decref
wasm-ld: error: /var/folders/bn/gbn9_dvx3vq3h4k_k2wm0k4w0000gp/T/emscripten_temp_h5tvwd58/module_0.o: undefined symbol: _emval_run_destructors
wasm-ld: error: /var/folders/bn/gbn9_dvx3vq3h4k_k2wm0k4w0000gp/T/emscripten_temp_h5tvwd58/module_0.o: undefined symbol: _emval_get_global
wasm-ld: error: /var/folders/bn/gbn9_dvx3vq3h4k_k2wm0k4w0000gp/T/emscripten_temp_h5tvwd58/module_0.o: undefined symbol: _emval_incref
wasm-ld: error: /var/folders/bn/gbn9_dvx3vq3h4k_k2wm0k4w0000gp/T/emscripten_temp_h5tvwd58/module_0.o: undefined symbol: _emval_new_cstring
wasm-ld: error: /Users/myuser/Documents/Emscripten/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/libstandalonewasm-nocatch.a(__main_void.o): undefined symbol: main
emcc: error: '/Users/myuser/Documents/Emscripten/emsdk/upstream/bin/wasm-ld -o module.wasm /var/folders/bn/gbn9_dvx3vq3h4k_k2wm0k4w0000gp/T/emscripten_temp_h5tvwd58/module_0.o -L/Users/myuser/Documents/Emscripten/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten /Users/myuser/Documents/Emscripten/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/crt1.o -lGL-getprocaddr -lal -lhtml5 -lstandalonewasm-nocatch -lstubs-debug -lc-debug -ldlmalloc -lcompiler_rt -lc++-noexcept -lc++abi-debug-noexcept -lsockets -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr /var/folders/bn/gbn9_dvx3vq3h4k_k2wm0k4w0000gp/T/tmp8lk5vxyulibemscripten_js_symbols.so --strip-debug --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_get_current --export=emscripten_stack_init --export=stackSave --export=stackRestore --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defined=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-table -z stack-size=65536 --no-growable-memory --initial-heap=16777216 --stack-first --table-base=1' failed (returned 1)
What did I do wrong?
If you use emscripten::val
you need to link with -lembind
. It looks like maybe you didn't do that?
Also, you probably want to have emscripten output the .js
file for you, especially if you are using embind/val. So you would do -o module.js
instead.
Finally, if you don't have a main function you can pass
--no-entry` to suppress any warnings/errors regarding that.