无法绑定emscripten中的功能 [英] Not able to bind function in emscripten
问题描述
我正在尝试使用emscripten从js调用我的c/c ++函数.为此,我指的是本教程: https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html#embind
I am trying to use emscripten to call my c/c++ function from js. For this, I am referring this tutorial : https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html#embind
我正在遵循本文中提到的过程,但是 lerp
函数没有导出到 Module
,并且我得到了 TypeError:Module.lerp不是功能
在我的浏览器控制台中.
I am following the process mentioned in this article but lerp
function is not getting exported to Module
and I am getting TypeError: Module.lerp is not a function
in my browser console.
我只是使用本文中提到的文件而没有进行任何修改,但是仍然无法从js调用c函数.
I am just using the files mentioned in this article without any modification but still failing to call c function from js.
请帮助我我所缺少的内容
Please help me what I am missing
// quick_example.cpp
#include <emscripten/bind.h>
using namespace emscripten;
float lerp(float a, float b, float t) {
return (1 - t) * a + t * b;
}
EMSCRIPTEN_BINDINGS(my_module) {
function("lerp", &lerp);
}
index.html
index.html
<!doctype html>
<html>
<script src="quick_example.js"></script>
<script>
console.log('lerp result: ' + Module.lerp(1, 2, 0.5));
</script>
</html>
构建说明:
emcc --bind -o quick_example.js quick_example.cpp
运行本地服务器
pyhton -m SimpleHTTPServer 9000
在浏览器上,启动此页面时,出现此错误.
On browser, when launching this page, I am getting this error.
TypeError: Module.lerp is not a function
谢谢
推荐答案
初始化尚未完成.
<!doctype html>
<html>
<script src="quick_example.js"></script>
<script>
Module['onRuntimeInitialized'] = () => {
console.log('lerp result: ' + Module.lerp(1, 2, 0.5));
}
</script>
</html>
这篇关于无法绑定emscripten中的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!