WebAssembly 编译C++代码

最近做WebAssembly的技术预言。

为何用WebAssembly:

https://zhuanlan.zhihu.com/p/53077277

WebAssembly技术原理,与jits对比的优势:

https://www.jianshu.com/p/bff8aa23fe4d

中文网站:

https://wasm.zcopy.site/

https://developer.mozilla.org/zh-CN/docs/WebAssembly

 

采用的教程

https://emscripten.org/docs/getting_started/downloads.html#installation-instructions

1. 下载工具

# Get the emsdk repo
git clone https://github.com/emscripten-core/emsdk.git

# Enter that directory
cd emsdk

2. 安装最新

git pull

# windows环境所以直接写 emsdk
# Download and install the latest SDK tools.
emsdk install latest

# Make the "latest" SDK "active" for the current user. (writes ~/.emscripten file)
emsdk activate latest

# 把emsdk加到环境变量中
# Activate PATH and other environment variables in the current terminal
emsdk_env.bat

如果修改了SDK的位置,那么需要重新执行 emsdk activate latest 和 emsdk_env.bat 指令

3. 写个简单的helloworld.c

#include <stdio.h>

int main() {
  printf("hello, world!\n");
  return 0;
}

4. 编译helloworld.c

./emcc tests/hello_world.c

会生成a.out.js 和 a.out.wasm 

5. 运行 node a.out.js会出现hello world表示生成的没问题。

6. 生成网页HTML

emcc tests/hello_world.c -o hello.html

7.网页显示,创建httpserver(chrome不支持本地文件方式),浏览器键入http://localhost:8000/hello.html,发现ok!

#创建httpserver
python -m http.server

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值