Web Assembly C++ (1) : helloworld

本文详细介绍如何在Ubuntu18上部署Emscripten环境,包括安装emsdk、指定版本的emsdk工具、CMake、Python2.7及Java等必要组件。同时,通过示例代码hello.c演示了如何使用emcc进行编译,并解决可能出现的ModuleNotFoundError问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境部署

确保python版本至少是2.7以上的(ubuntu 18默认的是python3,理论上可以,我测试的也可以)
Emscripten SDK - emsdk

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

# Enter that directory
cd emsdk

# Download and install the VERSION SDK tools.
# 可以安装一个最新版,也可以指定版本,为了后续的维护,建议指定版本
#./emsdk install latest
./emsdk install 1.38.45

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

# Activate PATH and other environment variables in the current terminal
source ./emsdk_env.sh

# Get a specific version using the upstream backend.
#./emsdk install latest-upstream

# Get a specific version using the fastcomp backend.
#./emsdk install 1.38.45-fastcomp

# Install CMake (optional, only needed for tests and building Binaryen)
sudo apt-get install cmake
sudo apt-get install python2.7
# Install Java (optional, only needed for Closure Compiler minification)
sudo apt-get install default-jre

Hello World

hello.c

#include <stdio.h>
int main(int argc, char ** argv) {
	printf("Hello, world!\n");
}
emcc hello.c -s WASM=1 -o hello.html

可能遇到的问题:
ModuleNotFoundError: No module named ‘distutils.spawn’
需要 python3-distutils

sudo apt-get install python3-distutils
emrun --no_browser --port 8080 .

或者使用python的

python -m http.server 8000

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值