首页 > 解决方案 > 使用 clang 将 C++ 库编译为 wasm 失败,并出现“stdlib.h”文件未找到错误

问题描述

我正在尝试将此示例代码从 rust 编译为 wasm。这是一个 rust crate,可让您创建/编写 excel 文件。我已经成功地使用cargo run.

不幸的是,当我尝试将其编译为 Web 程序集时,我遇到了一个错误,指出在编译bzip2-sys<stdlib.h>时找不到 library ,一个常见的 C 库,一个 rust crate。

当我第一次尝试编译它时,它提示我安装我从这里做的 clang (Windows 的预构建二进制文件(64 位和 32 位)),并且我已经包含C:\Program Files\LLVM\bin到系统 Path 环境变量中。我还安装了 MinGW-w64,以及带有 Visual C++ 构建工具的 Visual Studio 2017。

您可以在下面的 Visual Studio Code 中看到整个错误。

PS C:\Users\User\Desktop\excel-test> wasm-pack build
[INFO]: Checking for the Wasm target...
[INFO]: Compiling to Wasm...
   Compiling libc v0.2.71
   Compiling pkg-config v0.3.17
   Compiling cc v1.0.54
   Compiling crc32fast v1.2.0
   Compiling cfg-if v0.1.10
   Compiling adler32 v1.0.4
   Compiling podio v0.1.7
   Compiling miniz_oxide v0.3.6
   Compiling time v0.1.43
   Compiling flate2 v1.0.14
   Compiling bzip2-sys v0.1.9+1.0.8
error: failed to run custom build command for `bzip2-sys v0.1.9+1.0.8`

Caused by:
  process didnt exit successfully: `C:\Users\User\Desktop\excel-test\target\release\build\bzip2-sys-aa88969feaab6a44\build-script-build` (exit code: 1)
--- stdout
TARGET = Some("wasm32-unknown-unknown")
OPT_LEVEL = Some("3")
HOST = Some("x86_64-pc-windows-msvc")
CC_wasm32-unknown-unknown = None
CC_wasm32_unknown_unknown = None
TARGET_CC = None
CC = None
CFLAGS_wasm32-unknown-unknown = None
CFLAGS_wasm32_unknown_unknown = None
TARGET_CFLAGS = None
CFLAGS = None
CRATE_CC_NO_DEFAULTS = None
DEBUG = Some("false")
running: "clang" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "--target=wasm32-unknown-unknown" "-I" "bzip2-1.0.8" "-D_FILE_OFFSET_BITS=64" "-DBZ_NO_STDIO" "-o" "C:\\Users\\User\\Desktop\\excel-test\\target\\wasm32-unknown-unknown\\release\\build\\bzip2-sys-03c492d40657ce9f\\out\\lib\\bzip2-1.0.8/blocksort.o" "-c" "bzip2-1.0.8/blocksort.c"
cargo:warning=In file included from bzip2-1.0.8/blocksort.c:22:
cargo:warning=bzip2-1.0.8/bzlib_private.h:25:10: fatal error: 'stdlib.h' file not found
cargo:warning=#include <stdlib.h>
cargo:warning=         ^~~~~~~~~~
cargo:warning=1 error generated.
exit code: 1

--- stderr


error occurred: Command "clang" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "--target=wasm32-unknown-unknown" "-I" "bzip2-1.0.8" "-D_FILE_OFFSET_BITS=64" "-DBZ_NO_STDIO" "-o" "C:\\Users\\User\\Desktop\\excel-test\\target\\wasm32-unknown-unknown\\release\\build\\bzip2-sys-03c492d40657ce9f\\out\\lib\\bzip2-1.0.8/blocksort.o" "-c" "bzip2-1.0.8/blocksort.c" with args "clang" did not execute successfully (status code exit code: 1).



Error: Compiling your crate to WebAssembly failed
Caused by: failed to execute `cargo build`: exited with exit code: 101
  full command: "cargo" "build" "--lib" "--release" "--target" "wasm32-unknown-unknown"
PS C:\Users\User\Desktop\excel-test>

标签: crustclangwasm-packwebassembly

解决方案


假设文件是​​某个目录,您可能需要帮助 clang 找到它:

https://clang.llvm.org/docs/ClangCommandLineReference.html#include-path-management

控制#includes 如何解析为文件的标志。

-I, --include-directory , --include-directory=


推荐阅读