3

我阅读了有关 wasm 的本教程,在第 3 点之后。“调用 C 中定义的自定义函数”我试图弄清楚如何以这种方式将 C++ 函数和类绑定到 JavaScript?我必须在那里使用Embind,但是如何使用?

我还发现了这个线程,@lacenen 的第二个答案可能是一种解决方法。

4

1 回答 1

0

大多数时候你只需要在 WASM 中调用一个函数。所以简单的方法:

#ifdef __cplusplus
extern "C" {
#endif

void EMSCRIPTEN_KEEPALIVE MyFunc()
{
    printf("MyFunc()\n");
}

#ifdef __cplusplus
}
#endif

使用 EmScripten 进行编译,您将拥有一个定义了 Module 的 .js 文件。因此,在 JavaScript 方面,您可以使用 ccall ——这是没有参数或返回值的调用:

Module.ccall('MyFunc', null, null, null);

请参阅:https ://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#interacting-with-code-ccall-cwrap

于 2017-08-07T18:19:27.930 回答