简体   繁体   English

如何在 Emscripten Compiler 中嵌入多个 Cpp 文件?

[英]how to Embind multiple Cpp files in Emscripten Compiler?

i have two cpp files called in Main.cpp files this code to be called from ams.js files.我在 Main.cpp 文件中调用了两个 cpp 文件,此代码将从 ams.js 文件中调用。 im using Embind Compiler to call from js我使用 Embind Compiler 从 js 调用

here is my sample Code :这是我的示例代码:

class.h类.h

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

class.cpp类.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);

 }

For Compiling :编译:

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

Calling the Function in Render.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();

Output error:输出错误:

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

help me to solve this issue帮我解决这个问题

Use separate compilation.使用单独编译。

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

But BindingError is caused by new Module.MyClass();但是 BindingError 是由new Module.MyClass();引起的new Module.MyClass(); , try new Module.MyClass(123); , 试试new Module.MyClass(123); . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM