Emscripten介绍以及安装中的坑
最近Qt更新到5.13版本,加入了很多新功能,其中包括对WebAssembly的支持。Emscripten的作用就是充当将C/C++编译成符合WebAssembly标准的二进制编码的编译器。
什么是Emscripten
最近Qt更新到5.13版本,加入了很多新功能,其中包括对 WebAssembly 的支持,WebAssembly这个词相比开发者对其一定不陌生。直译过来叫做“网页字节码”,WebAssembly是一种标准,该标准的主要贡献者为W3C、Mozilla、Microsoft、Google、Apple这类国际大厂。
回到主题,什么是Emscripten? Emscripten 是这么描述的:
Emscripten is a toolchain for compiling to asm.js and WebAssembly, built using LLVM, that lets you run C and C++ on the web at near-native speed without plugins.
翻译下来就是:
Emscripten是一个工具链,作用是通过LLVM来编译生成asm.js、WebAssembly字节码,目的是让你能够在网页中接近最快的速度运行C和C++,并且不需要任何插件。
Emscripten的安装
Emscripten的安装需要下载外网的依赖,所以需要使用科学上网工具。
笔者使用MacOS安装Emscripten。 具体步骤如下:
- 通过Git的clone命令获取Emscripten源码包
# 获取emsdk包
git clone https://github.com/emscripten-core/emsdk.git
# 进入文件夹
cd emsdk
- 运行以下的emsdk指令去从Github获取最新的工具并激活工具
# 获取最新的emsdk版本
git pull
# 下载和安装最新的工具
./emsdk install latest
# 激活工具
./emsdk activate latest
# 激活环境变量(需设置环境变量,会有提示的)
source ./emsdk_env.sh
- 检查是否安装成功
emcc -v
xxs-MacBook-Pro:emsdk StupidZhe$ emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 1.38.38
clang version 6.0.1 (/b/s/w/ir/cache/git/chromium.googlesource.com-external-github.com-emscripten--core-emscripten--fastcomp--clang 98df4be387dde3e3918fa5bbb5fc43e1a0e1daac) (/b/s/w/ir/cache/git/chromium.googlesource.com-external-github.com-emscripten--core-emscripten--fastcomp 1b4148f39a69c7fc62edadd85e4122b68694dfb7) (emscripten 1.38.31 : 1.38.31)
Target: x86_64-apple-darwin18.6.0
Thread model: posix
InstalledDir: /Users/StupidZhe/IT/emsdk/fastcomp/fastcomp/bin
shared:INFO: (Emscripten: Running sanity checks)
ps:官网有各个系统安装的注意事项:https://emscripten.org/docs/getting_started/downloads.html
安装过程的坑
- 在执行命令 ./emsdk install latest 过程中会遇到如下错误:
xxxs-MacBook-Pro:emsdk StupidZhe$ emsdk install latest
Fetching emscripten-releases repository...
Repository 'https://chromium.googlesource.com/emscripten-releases' already cloned to directory '/Users/StupidZhe/IT/emsdk/releases', skipping.
Fetching latest changes to the branch 'master' for '/Users/StupidZhe/IT/emsdk/releases'...
Already up to date.
Successfully updated and checked out branch 'master' on repository '/Users/StupidZhe/IT/emsdk/releases'
Current repository version: "Fri, 12 Jul 2019 01:21:37 +0000 25df940fc1647cd6fec3593eeb9a439e2b3d1c0d"
Fetching all precompiled tagged releases..
Error downloading URL 'https://s3.amazonaws.com/mozilla-games/emscripten/packages/llvm/tag/osx_32bit/index.txt':
Error downloading URL 'https://s3.amazonaws.com/mozilla-games/emscripten/packages/llvm/tag/osx_64bit/index.txt':
Installing SDK 'sdk-releases-fastcomp-80bff2784f8500c1305ca69ba1d9fc84df0e401c-64bit'..
Installing tool 'releases-fastcomp-80bff2784f8500c1305ca69ba1d9fc84df0e401c-64bit'..
Error downloading URL 'https://storage.googleapis.com/webassembly/emscripten-releases-builds/mac/80bff2784f8500c1305ca69ba1d9fc84df0e401c/wasm-binaries.tbz2':
Traceback (most recent call last):
File "/Users/StupidZhe/IT/emsdk/emsdk", line 2697, in
sys.exit(main())
File "/Users/StupidZhe/IT/emsdk/emsdk", line 2679, in main
success = tool.install()
File "/Users/StupidZhe/IT/emsdk/emsdk", line 1540, in install
success = tool.install()
File "/Users/StupidZhe/IT/emsdk/emsdk", line 1556, in install
success = download_and_unzip(url, self.installation_path(), download_even_if_exists=download_even_if_exists, filename_prefix=filename_prefix)
File "/Users/StupidZhe/IT/emsdk/emsdk", line 1116, in download_and_unzip
assert received_download_target == download_target
AssertionError
解决方法:
使用文本工具打开emsdk,在其源码中加入两行命令:
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
- 一开始就卡住了。
解决方法:
使用科学上网工具。