webassembly-cn / pit Goto Github PK
View Code? Open in Web Editor NEW在玩 WebAssembly 时,遇到的坑!提交到issue
在玩 WebAssembly 时,遇到的坑!提交到issue
镜像 ubuntu:latest
环境准备, 参考
$ apt update
$ apt install git cmake gcc python
编译
$ git clone https://github.com/juj/emsdk.git
$ cd emsdk
$ ./emsdk install sdk-incoming-64bit binaryen-master-64bit
报错
CMake Error at CMakeLists.txt:47 (project): No CMAKE_CXX_COMPILER could be found. Tell CMake where to find the compiler by setting either the environment variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path to the compiler, or to the compiler name if it is in the PATH. -- Configuring incomplete, errors occurred! See also "/root/emsdk/clang/fastcomp/build_incoming_64/CMakeFiles/CMakeOutput.log". See also "/root/emsdk/clang/fastcomp/build_incoming_64/CMakeFiles/CMakeError.log". CMake invocation failed due to exception! Working directory: /root/emsdk/clang/fastcomp/build_incoming_64 Command '['cmake', '-G', 'Unix Makefiles', '-DCMAKE_BUILD_TYPE=RelWithDebInfo', '-DPYTHON_EXECUTABLE=/usr/bin/python', '-DLLVM_TARGETS_TO_BUILD=X86;JSBackend', '-DLLVM_INCLUDE_EXAMPLES=OFF', '-DCLANG_INCLUDE_EXAMPLES=OFF', '-DLLVM_INCLUDE_TESTS=OFF', '-DCLANG_INCLUDE_TESTS=OFF', '-DLLVM_ENABLE_ASSERTIONS=ON', '/root/emsdk/clang/fastcomp/src']' returned non-zero exit status 1 Installation failed!
解决
官方的入门指南里只提到gcc,但实际也依赖 g++。一步到位可以安装 build-essential
,想要精简可以只安装 cmake
gcc
和 g++
所以,安装 g++ 后重新编译
$ apt install g++
解决。
官网安装 emsdk 的方法比较慢,可以直接安装 emscripten brew install emscripten
.
安装完底部有提示需要修改手动修改 ~/.emscripten
.
将
LLVM_ROOT = os.path.expanduser(os.getenv('LLVM') or '/usr/bin') # directory
BINARYEN_ROOT = os.path.expanduser(os.getenv('BINARYEN') or '') # directory
修改成
LLVM_ROOT = os.path.expanduser(os.getenv('LLVM') or '/usr/local/opt/emscripten/libexec/llvm/bin') # directory
#BINARYEN_ROOT = os.path.expanduser(os.getenv('BINARYEN') or '') # directory
再运行 emcc -v
在编译过程中,如 emcc hello.c -s WASM=1 -o hello.html
,可能会报 OSError: [Errno 2] No such file or directory
的错误,安装 brew install cmake
搞定!
运行
emcc -s WASM=1 -O3 -o program.js program.cpp --js-library library.js
返回
warning: unresolved symbol: _Z6funcNamev
program.js
#include <emscripten/emscripten.h>
#include <stack>
// 定义数组大小
#define N 100
// 防止 C++ 函数名被 Mangling
#ifdef _cplusplus
extern "C" {
#endif
// 预置函数,暴露给 JS 进行处理
extern void checku ();
void EMSCRIPTEN_KEEPALIVE abc () {
char a[] = "username";
checku();
}
#ifdef _cplusplus
}
#endif
library.js
mergeInto(LibraryManager.library, {
checku: function() {
console.log(123)
}
});
笔误,踩了一个大坑。
#ifdef _cplusplus
extern "C" {
#endif
其实是 __cplusplus
前面两个下划线
#ifdef __cplusplus
extern "C" {
#endif
添加这个判断防止 C++ 函数名被 Mangling.
出现
urllib2.URLError: <urlopen error [Errno 60] Operation timed out>
或者
urllib2.URLError: <urlopen error [Errno 61] Connection refused>
检查当前网络是否能通
是否可以访问https://s3.amazonaws.com/mozilla-games/emscripten/packages/llvm/nightly/linux_32bit/index.txt
然后检查下wget命令是否能正常执行
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.