Not able to bind function in emscripten

谁都会走 提交于 2021-02-11 14:56:26

问题


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

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.

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

<!doctype html>
<html>
  <script src="quick_example.js"></script>
  <script>
    console.log('lerp result: ' + Module.lerp(1, 2, 0.5));
  </script>
</html>

build instruction :

emcc --bind -o quick_example.js quick_example.cpp

run local server

pyhton -m SimpleHTTPServer 9000

On browser, when launching this page, I am getting this error.

TypeError: Module.lerp is not a function

Thanks


回答1:


Initialization isn't complete yet.

<!doctype html>
<html>
  <script src="quick_example.js"></script>
  <script>
  Module['onRuntimeInitialized'] = () => {
    console.log('lerp result: ' + Module.lerp(1, 2, 0.5));
  }
  </script>
</html>



回答2:


I think you need to put your lerp function in the EXPORTED_FUNCTIONS command line option

EXPORTED_FUNCTIONS=['lerp']

Also, you may want to use the EMSCRIPTEN_KEEPALIVE annotation in your code to prevent inlining, but EXPORTED_FUNCTIONS should be enough. See:

https://kripken.github.io/emscripten-site/docs/getting_started/FAQ.html#why-do-functions-in-my-c-c-source-code-vanish-when-i-compile-to-javascript-and-or-i-get-no-functions-to-process



来源:https://stackoverflow.com/questions/51343425/not-able-to-bind-function-in-emscripten

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!