javascript - 我应该在 JS 中哪里定义 emscripten extern 函数?

标签 javascript emscripten asm.js

假设我在 C++ 中将函数 x 定义为:

extern "C" void x();

我在全局上下文中用 JS 实现了它

function _x() { console.log('x called'); }

_x 是在 asm 编译的 js 文件中定义的,该文件正在被调用,而不是我的实现。我做错了什么?

我在链接时收到此警告:

warning: unresolved symbol: x

这是堆栈跟踪:

Uncaught abort() at Error
at jsStackTrace (http://localhost/module.js:978:13)
at stackTrace (http://localhost/module.js:995:22)
at abort (http://localhost/module.js:71106:25)
at _x (http://localhost/module.js:5829:46)
at Array._x__wrapper (http://localhost/module.js:68595:41)
at Object.dynCall_vi (http://localhost/module.js:68442:36)
at invoke_vi (http://localhost/module.js:7017:25)
at _LoadFile (http://localhost/module.js:7573:6)
at asm._LoadFile (http://localhost/module.js:69219:25)
at eval (eval at cwrap (http://localhost/module.js:554:17), <anonymous>:6:26)

最佳答案

Implement a C API in Javascript 中所述,您可以通过一个 Javascript 文件来定义一个库,该文件调用 mergeInto 将对象与您的 Javascript 函数合并到 LibraryManager.library 中。然后,您可以使用 --js-library 选项进行编译以传入库的位置。

例如,您可以将 Javascript 文件与单个函数的库一起使用

// In library.js
mergeInto(LibraryManager.library, {
  x: function() {
    alert('hi');
  },
});

调用此函数的主 C++ 文件

// in librarytest.cc
extern "C" void x();

int main()
{
  x();
  return 0;
}

并使用

编译为 HTML 和 Javascript
em++ librarytest.cc --js-library library.js -o librarytest.html

然后您在浏览器中加载librarytest.html,它将显示一个带有hi的警告框。

关于javascript - 我应该在 JS 中哪里定义 emscripten extern 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33673575/

相关文章:

javascript - 在带有 Emscripten 的 Nim 中使用字符串会导致 JavaScript 错误

javascript - 在 jqgrid 的编辑模式下,我可以让光标默认从特定列开始吗?

c++ - 在Firefox中执行WASM时, “debug trap handling”是什么

javascript - 是否可以只使用 AngularJS 的前端脚本?

c++ - 如何使用 SDL 将纹理加载到使用 emscripten 的 WebAssembly 项目中?

c++ - 如何将 emscripten 与 cmake 一起用于项目依赖项?

emscripten - 试图让 asm.js 返回一个类型化数组

rust、WebAssembly 和传递参数以增加总内存

javascript - 如何使用 JavaScript 将 DateTime 的 C# 输出转换为 "Days Remaining"?

javascript - 将成员函数和变量从一个对象复制到另一个对象时,内存方面会发生什么