【问题标题】:Import External Symbol with Clang and WebAssembly使用 Clang 和 WebAssembly 导入外部符号
【发布时间】:2021-01-08 18:13:13
【问题描述】:

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

#ifdef __cplusplus
    extern "C" {
#endif

extern void externalFunction(void);

#ifdef __cplusplus
    }
#endif

int main() {
    externalFunction();
    return 0;
}

【问题讨论】:

    标签: c++ c clang linker-errors webassembly


    【解决方案1】:

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

    __attribute__((import_module("env"), import_name("externalFunction"))) void externalFunction(void);
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-26
      • 1970-01-01
      • 1970-01-01
      • 2021-05-31
      • 2020-10-05
      • 2023-03-13
      • 2017-03-02
      • 1970-01-01
      相关资源
      最近更新 更多