![](/img/trans.png)
[英]How to use the .js and .wasm artifacts emitted by Emscripten and Embind?
[英]how to Embind multiple Cpp files in Emscripten Compiler?
我在 Main.cpp 文件中调用了两个 cpp 文件,此代码将从 ams.js 文件中调用。 我使用 Embind Compiler 从 js 调用
这是我的示例代码:
类.h
class CLASS{
public:
int VARIABLE;
void FUNCTION();
};
类.cpp
#include "CLASS.h"
void CLASS::FUNCTION()
{
VARIABLE = 5;
std::cout << "out : "+VARIABLE << std::endl;
}
主程序
#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();
, 试试new Module.MyClass(123);
.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.