使用 Clang 和 WebAssembly 导入外部符号

pro*_*mer 5 c c++ clang linker-errors webassembly

extern当使用 Clang将具有函数声明的 C(++) 代码编译到 WebAssembly 时,它会给出undefined reference错误而不是导入符号。什么选项将使链接器将它们添加为 WebAssembly 模块的导入,而不是尝试解析它们?

#ifdef __cplusplus
    extern "C" {
#endif

extern void externalFunction(void);

#ifdef __cplusplus
    }
#endif

int main() {
    externalFunction();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

sbc*_*100 10

您可以使用 import_module 和 import_name clang 属性将函数标记为已导入。例如:

__attribute__((import_module("env"), import_name("externalFunction"))) void externalFunction(void);
Run Code Online (Sandbox Code Playgroud)

或者您可以将 --allow-undefined 传递给链接器。请参阅https://lld.llvm.org/WebAssembly.html#imports