1. 程式人生 > >Emscripten介紹以及安裝中的坑

Emscripten介紹以及安裝中的坑

Emscripten介紹以及安裝中的坑

最近Qt更新到5.13版本,加入了很多新功能,其中包括對WebAssembly的支援。Emscripten的作用就是充當將C/C++編譯成符合WebAssembly標準的二進位制編碼的編譯器。

什麼是Emscripten

最近Qt更新到5.13版本,加入了很多新功能,其中包括對 <a href="https://en.wikipedia.org/wiki/WebAssembly"> WebAssembly </a> 的支援,WebAssembly這個詞相比開發者對其一定不陌生。直譯過來叫做“網頁位元組碼”,WebAssembly是一種標準,該標準的主要貢獻者為W3C、Mozilla、Microsoft、Google、Apple這類國際大廠。

回到主題,什麼是Emscripten? <a href="https://emscripten.org"> Emscripten官網 </a> 是這麼描述的:

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。 具體步驟如下:

  1. 通過Git的clone命令獲取Emscripten原始碼包
# 獲取emsdk包
git clone https://github.com/emscripten-core/emsdk.git

# 進入資料夾
cd emsdk
  1. 執行以下的emsdk指令去從Github獲取最新的工具並激活工具
# 獲取最新的emsdk版本
git pull

# 下載和安裝最新的工具
./emsdk install latest

# 啟用工具
./emsdk activate latest

# 啟用環境變數(需設定環境變數,會有提示的)
source ./emsdk_env.sh
  1. 檢查是否安裝成功
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

安裝過程的坑

  1. 在執行命令 ./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': <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)>
Error downloading URL 'https://s3.amazonaws.com/mozilla-games/emscripten/packages/llvm/tag/osx_64bit/index.txt': <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)>
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': <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)>
Traceback (most recent call last):
  File "/Users/StupidZhe/IT/emsdk/emsdk", line 2697, in <module>
    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
  1. 一開始就卡住了。

解決方法:使用