Javascript 找不到使用 emscripten 编译的导出 WASM 函数
Javascript cannot find exported WASM functions compiled with emscripten
我有一些c++文件,其中包含我试图编译为wasm的函数。出于某种原因,即使文件被导出的函数编译为 emscripten,当我尝试使用这些函数时,我不断收到"TypeError:___ 不是一个函数"。
这是包含我要导出的所有函数的"bridge.cpp"文件:
/*
* Bridge from JS into the cellmap computation code in Wasm
*/
#include <gameoflife.h>
#include <constants.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
/**
* A struct that manages our instantiation of
* the game of life
*/
typedef struct {
cellmap *cm; /** holds the instance of the cellmap */
unsigned char *cells; /** holds the cell vals for current iteration */
} GOL_Instance;
/**
* Constructor for the game of life engine
*/
GOL_Instance *
GOL_Instance_new() {
GOL_Instance *gol;
gol = (GOL_Instance *)malloc(sizeof *gol);
if (gol) {
memset(gol, 0, sizeof *gol);
}
gol->cm = new_cellmap();
return gol;
}
/**
* Method to destroy the gol instance when the user is done
*/
void
GOL_Instance_destroy(GOL_Instance *gol) {
gol->cm->~cellmap();
delete[] gol->cells;
free(gol);
}
/**
* Method to initialize the gol instance
*/
void
GOL_Init(GOL_Instance *gol) {
gol->cm->init();
gol->cells = gol->cm->return_cells();
}
/**
* Method to run the gol instance by one step
*/
void
GOL_Step(GOL_Instance *gol) {
gol->cm->next_generation();
gol->cells = gol->cm->return_cells();
}
/**
* Method to get the cell values
*/
void
GOL_get_values(GOL_Instance *gol, int8_t *arr) {
unsigned char *c = gol->cells;
unsigned int total_cells = cellmap_width * cellmap_height;
for (unsigned int i = 0; i < total_cells; i++) {
unsigned int curr_ind = i * 8;
int8_t curr = (int8_t) *(c+i) & 0x01;
*(arr + curr_ind) = curr;
}
}
然后我使用 emscripten 来编译代码:
em++ -s WASM=1 -s ALLOW_MEMORY_GROWTH=1
-Igameoflife/include -Os -DNDEBUG
-s EXPORTED_FUNCTIONS="['_GOL_Instance_new', '_GOL_Instance_destroy', '_GOL_Init', '_GOL_Step', '_GOL_get_values']"
-o gol.js gameoflife/src/cellmap.cpp bridge.cpp
这个命令给了我文件'gol.js'和'gol.wasm',它们似乎编译没有任何错误。 但是,当我在我的 javascript 中尝试实际使用给定的功能时(注意:我在 SVG 文件中使用这些函数,这就是为什么我的"href"标签在不同的命名空间下(:
<script xlink:href="gol.js"></script>
<script><![CDATA[
var wasm_loaded = false;
var gol_instance;
const cols = 96;
const rows = 96;
var cellArr = new Int8Array(cols * rows);
const box_side = 10; // boxes are 10 pixels accross
Module.onRuntimeInitialized = function() {
wasm_loaded = true;
gol_instance = Module._GOL_Instance_new();
}
.
.
.
然后在 chrome 的控制台输出中,当我在本地托管页面时,我得到 wasm 流式编译失败:类型错误:Module._GOL_Instance_new 不是函数
和JavaScript崩溃。
我尝试使用"-s EXPORT_ALL=1"进行编译,但仍然找不到该函数。此外,"gol.wasm"文件似乎异常小(只有 12 个字符(。我不确定这是否表明问题,但我认为这可能是相对的。
将以下内容添加到构建命令-s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]'
应该可以解决错误
对于那些现在遇到这个问题的人:
C++对其符号进行名称重整,因此,导出的名称将与源文件中声明的名称不同。
为了绕过此行为,您需要用extern "C" {}
包装要导出的函数
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 内置函数可查看CPP中的成员变量
- Javascript 找不到使用 emscripten 编译的导出 WASM 函数
- 尝试运行 wasm 函数时出现模块未定义错误
- 如何将图像帧相机传递给wasm (C++)中的函数?