问题描述
我无法在另一个JavaScript文件中调用C函数,它给出了错误“在运行时初始化之前调用” please refer to this link
我按照给定链接中的描述在emscripten中编译了C代码,并在test.js文件中使用了生成的asm.js文件。 用于生成asm的命令:-
emcc test/hello.cpp -o hello.html -s EXPORTED_FUNCTIONS="['_int_sqrt']" -s EXPORTED_RUNTIME_METHODS="["ccall","cwrap"]"
var Module = require('./asm.js');
var test = Module.cwrap('int_sqrt','number',['number']);
console.log(test(25));
当我运行node test
时出现错误
abort(Assertion Failed: native function `int_sqrt` called before runtime initialization)
解决方法
您应该等待运行时初始化。
试试这个:
var Module = require("./lib.js");
var result = Module.onRuntimeInitialized = () => {
Module.ccall('myFunction',// name of C function
null,// return type
null,// argument types
null // arguments
);
}
,
我在使用 emscripten 时遇到了同样的问题,这对我有用:
<script>
Module.onRuntimeInitialized = () => { Module.functionName(param); }
</script>
其中 functionName 是您要调用的函数的名称,param 是您要传递给它的值。