javascript - 未解析的符号 : llvm_trap from Emscripten

标签 javascript c emscripten webassembly

当我尝试将以下代码片段编译为 WebAssembly 二进制文件时,我不断遇到 未解析的符号:llvm_trap 警告,这使得 JS 无法使用 wasm 代码。

emcc test.c -s WASM=1 -s ONLY_MY_CODE=1 -s "EXPORTED_FUNCTIONS=['_test']" -O2 -g -o test.js

test.c(这是一个测试代码,用于重现问题而不执行有意义的工作。)

int test(int *buf) {
  int C = 1;
  // Assuming WebAssembly.Memory buffer has been preloaed with data. 
  // *T represents the preloaded data here. And We know *T and *buf 
  // won't overlap in memory.
  int *T = 0; 

  int index = C ^ buf[5];
  int right = T[index];
  int left = (unsigned)C >> 8;

  // warning disappears if this is commented out. But why?
  C = left ^ right; 

  return C;
}

我没有编写任何llvm_trap相关代码。有人知道这意味着什么吗?

最佳答案

变量T必须初始化。如果它表示“映射”到 WebAssembly 线性内存的数组,您可以将其定义为全局数组,如下所示:

int T[1000];

int test(int *buf) {
  int C = 1;

  int index = C ^ buf[5];
  int right = T[index];
  int left = (unsigned)C >> 8;

  // warning disappears if this is commented out. But why?
  C = left ^ right;

  return C;
}

编译时不会出现 llvm_trap 警告。

有关如何使用线性内存将数据传递到 WASM 函数的更多详细信息,请参阅以下问题:

How to access WebAssembly linear memory from C/C++

关于javascript - 未解析的符号 : llvm_trap from Emscripten,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47171012/

相关文章:

javascript - 无法重置 noUiSlider

javascript - 如何从 setTimeout 中获取另一个作用域中设置的变量?

c - 如何计算数组中的非零元素?

javascript - 如何将缩小的div放置在父div的中心内

c - 如何在 C 中使用指向结构的指针从结构中打印成员数据

c - 引用 C 中不带前导下划线的外部符号

c++ - 具有原始数据指针和 emscripten 的对象

c++ - CMake 制作的 Emscripten 库无法链接

c++ - 我可以让 emcc 支持初始化列表吗?

Javascript 命名函数定义在不应该执行的时候执行