Emscripten 在 macOS 上编译 C++:错误 "No thread API"

Emscripten compile C++ on macOS: error "No thread API"

我正在尝试使用 emscripten 编译一个相对简单的 C++ 程序,以便在 javascript (wasm) 中使用。当我使用以下设置编译时

emcc ./lttb.cpp \
  --bind \
  --target=wasm32-unknown-unknown-wasm \
  --optimize=3 \
  -v \
  -nostdlib \
  -Wl,--export-all \
  -Wl,--no-entry \
  -Wl,--allow-undefined \
  -s ALLOW_MEMORY_GROWTH=1 \
  -o lttb.wasm 

我收到以下错误:

In file included from ./lttb.cpp:3:
In file included from /usr/local/Cellar/emscripten/2.0.12/libexec/system/include/emscripten/bind.h:14:
In file included from /usr/local/Cellar/emscripten/2.0.12/libexec/system/include/libcxx/stddef.h:39:
/usr/local/Cellar/emscripten/2.0.12/libexec/system/include/libcxx/__config:1134:6: error: "No thread API"
#    error "No thread API"

更多详情here

您很可能在 lttb.cpp

内创建线程

在这种情况下,您必须添加: -s USE_PTHREADS=1

也建议提前定义线程数,例如: -s PTHREAD_POOL_SIZE=7

通过将构建参数更改为以下内容来修复它

rm lttb.wasm
emcc ./lttb.cpp \
  --bind \
  --optimize=3 \
  -Wl,--export-all \
  -Wl,--allow-undefined \
  -s ALLOW_MEMORY_GROWTH=1 \
  --no-entry \
  -o lttb.wasm

我相信是 -nostdlib 导致了这个特定问题(注意:原始构建参数是针对我正在制作的 C wasm,因此需要重新配置