如何在Emscripten编译器中启用多个CPP文件



我在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!

如何解决此问题?

使用单独的汇编。

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);

最新更新