gpt4 book ai didi

javascript - 如何在 Emscripten 编译器中嵌入多个 cpp 文件?

转载 作者:行者123 更新时间:2023-12-03 02:53:15 26 4
gpt4 key购买 nike

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

关于javascript - 如何在 Emscripten 编译器中嵌入多个 cpp 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47747690/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com