使用 emscripten 将 c++ 文件转换为 wasm 时出错

Posted

技术标签:

【中文标题】使用 emscripten 将 c++ 文件转换为 wasm 时出错【英文标题】:Error converting c++ file to wasm using emscripten 【发布时间】:2019-11-12 08:28:20 【问题描述】:

这是这篇帖子Converting .cpp file to .wasm file的后续问题

我正在尝试在 linux 上使用 emscripten 将我的 c++ 文件转换为 .wasm。我遇到了这个错误:

/home/maria/emsdk/upstream/lib/clang/10.0.0/include/ia32intrin.h:188:10: error: use of undeclared identifier '__builtin_ia32_readeflags_u32'
  return __builtin_ia32_readeflags_u32();
         ^
/home/maria/emsdk/upstream/lib/clang/10.0.0/include/ia32intrin.h:194:3: error: use of undeclared identifier '__builtin_ia32_writeeflags_u32'
  __builtin_ia32_writeeflags_u32(__f);
  ^
/home/maria/emsdk/upstream/lib/clang/10.0.0/include/ia32intrin.h:284:10: error: use of undeclared identifier '__builtin_ia32_crc32qi'
  return __builtin_ia32_crc32qi(__C, __D);
         ^
/home/maria/emsdk/upstream/lib/clang/10.0.0/include/ia32intrin.h:305:10: error: use of undeclared identifier '__builtin_ia32_crc32hi'
  return __builtin_ia32_crc32hi(__C, __D);
         ^
/home/maria/emsdk/upstream/lib/clang/10.0.0/include/ia32intrin.h:326:10: error: use of undeclared identifier '__builtin_ia32_crc32si'
  return __builtin_ia32_crc32si(__C, __D);
         ^
/home/maria/emsdk/upstream/lib/clang/10.0.0/include/ia32intrin.h:354:10: error: use of undeclared identifier '__builtin_ia32_rdpmc'
  return __builtin_ia32_rdpmc(__A);
         ^
/home/maria/emsdk/upstream/lib/clang/10.0.0/include/ia32intrin.h:360:10: error: use of undeclared identifier '__builtin_ia32_rdtscp'
  return __builtin_ia32_rdtscp(__A);
         ^
/home/maria/emsdk/upstream/lib/clang/10.0.0/include/ia32intrin.h:369:3: error: use of undeclared identifier '__builtin_ia32_wbinvd'
  __builtin_ia32_wbinvd();
  ^
In file included from sealdemo.cpp:1:
In file included from SEAL/native/src/seal/seal.h:6:
In file included from SEAL/native/src/seal/biguint.h:9:
In file included from SEAL/native/src/seal/memorymanager.h:10:
In file included from SEAL/native/src/seal/util/defines.h:62:
In file included from SEAL/native/src/seal/util/clang.h:18:
In file included from /home/maria/emsdk/upstream/lib/clang/10.0.0/include/x86intrin.h:15:
In file included from /home/maria/emsdk/upstream/lib/clang/10.0.0/include/immintrin.h:14:
/home/maria/emsdk/upstream/lib/clang/10.0.0/include/mmintrin.h:33:5: error: use of undeclared identifier '__builtin_ia32_emms'; did you mean '__builtin_isless'?
    __builtin_ia32_emms();
    ^
/home/maria/emsdk/upstream/lib/clang/10.0.0/include/mmintrin.h:33:5: note: '__builtin_isless' declared here
/home/maria/emsdk/upstream/lib/clang/10.0.0/include/mmintrin.h:33:25: error: too few arguments to function call, expected 2, have 0
    __builtin_ia32_emms();
                        ^
/home/maria/emsdk/upstream/lib/clang/10.0.0/include/mmintrin.h:50:19: error: use of undeclared identifier '__builtin_ia32_vec_init_v2si'
    return (__m64)__builtin_ia32_vec_init_v2si(__i, 0);
                  ^
/home/maria/emsdk/upstream/lib/clang/10.0.0/include/mmintrin.h:67:12: error: use of undeclared identifier '__builtin_ia32_vec_ext_v2si'
    return __builtin_ia32_vec_ext_v2si((__v2si)__m, 0);
           ^
/home/maria/emsdk/upstream/lib/clang/10.0.0/include/mmintrin.h:129:19: error: use of undeclared identifier '__builtin_ia32_packsswb'
    return (__m64)__builtin_ia32_packsswb((__v4hi)__m1, (__v4hi)__m2);
                  ^
/home/maria/emsdk/upstream/lib/clang/10.0.0/include/mmintrin.h:159:19: error: use of undeclared identifier '__builtin_ia32_packssdw'
    return (__m64)__builtin_ia32_packssdw((__v2si)__m1, (__v2si)__m2);
                  ^
/home/maria/emsdk/upstream/lib/clang/10.0.0/include/mmintrin.h:189:19: error: use of undeclared identifier '__builtin_ia32_packuswb'
    return (__m64)__builtin_ia32_packuswb((__v4hi)__m1, (__v4hi)__m2);
                  ^
/home/maria/emsdk/upstream/lib/clang/10.0.0/include/mmintrin.h:216:19: error: use of undeclared identifier '__builtin_ia32_punpckhbw'
    return (__m64)__builtin_ia32_punpckhbw((__v8qi)__m1, (__v8qi)__m2);
                  ^
/home/maria/emsdk/upstream/lib/clang/10.0.0/include/mmintrin.h:239:19: error: use of undeclared identifier '__builtin_ia32_punpckhwd'
    return (__m64)__builtin_ia32_punpckhwd((__v4hi)__m1, (__v4hi)__m2);
                  ^
/home/maria/emsdk/upstream/lib/clang/10.0.0/include/mmintrin.h:260:19: error: use of undeclared identifier '__builtin_ia32_punpckhdq'
    return (__m64)__builtin_ia32_punpckhdq((__v2si)__m1, (__v2si)__m2);
                  ^
/home/maria/emsdk/upstream/lib/clang/10.0.0/include/mmintrin.h:287:19: error: use of undeclared identifier '__builtin_ia32_punpcklbw'
    return (__m64)__builtin_ia32_punpcklbw((__v8qi)__m1, (__v8qi)__m2);
                  ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
shared:ERROR: '/home/maria/emsdk/upstream/bin/clang++ -target wasm32-unknown-emscrip

谁能帮忙

【问题讨论】:

ia32 说所有架构都混在一起了。 @Lee 对不起,你能解释一下吗? 关于 _builtin_ia32* 文档说:“这些内置函数可用于 i386 和 x86-64 系列计算机,具体取决于所使用的命令行开关。 "但是您正在编译为 wasm,因此这些函数不应该存在于此上下文中。 @Lee 你会碰巧知道解决方法吗?我是否放弃 emscripten 并使用其他东西?我有点迷路了。 我从未使用过 sealdemo.cpp,但我认为最好从那里开始,或者设置一些编译器标志。但是您的日志中没有包含这方面的信息。 【参考方案1】:

在为 WebAssembly 构建时,您不能/不应该包含 x86intrin.h。看起来你需要在没有SEAL_USE_INTRIN 的情况下构建:https://github.com/microsoft/SEAL/blob/master/native/src/seal/util/clang.h#L18

【讨论】:

【参考方案2】:

我已经能够使用带有内在函数 (#include <wasm_simd128.h>) 的 emscripten 将 Microsoft SEAL 库编译为 WebAssembly,但这是可选的。

工具链本身从来没有给我这个特定的错误,因为emcmake 步骤总是无法找到必要的头文件并且在没有优化的情况下编译。

听起来您可能没有使用适当的 emcmakeem++ 参数。此外,如果你想使用内在函数,你需要在 gcc.h 和 clang.h(位于 SEAL/native/src/seal/util/)中使用一些条件来使用 wasm_simd128.h 而不是 x86intrin.h

随时查看我的实现和/或构建在 Mac 和 Linux (Ubuntu) 上编译的脚本: https://github.com/morfix-io/node-seal

如果你只想要一个工作库,或者下载 npm 包: https://www.npmjs.com/package/node-seal

【讨论】:

以上是关于使用 emscripten 将 c++ 文件转换为 wasm 时出错的主要内容,如果未能解决你的问题,请参考以下文章

CMake可以生成配置文件吗?

将 C 代码转换为 Web 程序集时出错

如何使用 Emscripten 将对象从 Javascript 传递到 C++

2021-09-13 使用Emscripten 将c++编译为WebAssembly给JS调用

使用 emscripten 将 c++ 代码编译为 javascript 以求两个数之和。练习

Emscripten 将 C++ 编译为 JavaScript 和 Asm.js 的结果不佳