【问题标题】:How to Embind multiple cpp files in the Emscripten compiler?如何在 Emscripten 编译器中嵌入多个 cpp 文件?
【发布时间】:2017-12-11 06:45:51
【问题描述】:

我在 Main.cpp 文件中调用了两个 cpp 文件。此代码将从 ams.js 文件中调用。我正在使用 Embind 编译器从 JS 调用 WASM。

这是我的示例代码:

class.h:

class CLASS{
public:
int VARIABLE;
void FUNCTION();
};

class.cpp:

#include "CLASS.h"
void CLASS::FUNCTION()
{
VARIABLE = 5;

std::cout << "out : "+VARIABLE << std::endl;
}

Main.cpp:

#include <emscripten/bind.h>
#include "CLASS.h"
using namespace emscripten;
class MyClass
{
public:
MyClass(int x)
: x(x)

{}
int getCharCount(std::string strKey)
{
CLASS a;
a.FUNCTION();

return 0;

}

private:
int x;

 };

EMSCRIPTEN_BINDINGS(my_class_example) {

 class_<MyClass>("MyClass")
 .constructor<int>()
 .function("getCharCount", &MyClass::getCharCount);
    
 }

编译:

emcc --bind Main.cpp -o main.js

在 Render.js 中调用函数:

  var instance = new Module.MyClass();
  if (instance){
  var mainee  =  instance.getCharCount("hi")
  console.log("Somrthing is There");
  }else{
  console.log("Somrthing Wrong");
  }
  instance.delete();

输出错误:

main3.js:2780 Uncaught BindingError: Tried to invoke ctor of MyClass with invalid number of parameters (0) - expected (1) parameters instead!

我该如何解决这个问题?

【问题讨论】:

    标签: javascript compiler-errors emscripten embind


    【解决方案1】:

    使用单独编译。

    emcc --bind -c class.cpp
    emcc --bind -c main.cpp
    emcc --bind class.o main.o -o main.js
    

    但是BindingError是由new Module.MyClass();引起的,试试new Module.MyClass(123);

    【讨论】:

    • hii 感谢您对一个大模块的回复,例如我有一个模块包含 100 个 cpp 文件,我尝试了你的方法 find 在最后阶段我无法生成 js 文件它得到链接错误如何修复那个兄弟
    • ok 给我一些想法,即如何在编译时链接我的库,要清楚我将所有 cpp 文件转换为 .o 文件,然后我有两个库调用 configure.lib 和 curl.lib 如何由于未包含 lib 文件,我是否将所有内容编译到单个输出文件中,我的调用失败了
    • 您可能需要从源代码构建 curl.lib 和 configure.lib,因为 emscripten 不为 web 提供这些库。对于 curl,您可以编写新函数并将其映射到 ajax 调用(当您为 web 编译时)。我不确定您的 configure.lib 用例。只是添加 curl.lib 和 configure.lib 不会像 emscripten 一样工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 2011-03-13
    • 2020-08-23
    相关资源
    最近更新 更多