栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++之WebAssembly入门

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++之WebAssembly入门

相关资源

webassembly使用的帮助文档

Emscripten 下载和安装
# Get the emsdk repo
git clone https://github.com/emscripten-core/emsdk.git

# Enter that directory
cd emsdk

# Fetch the latest version of the emsdk (not needed the first time you clone)
git pull

# 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

# Activate PATH and other environment variables in the current terminal
# Windows 下运行emsdk_env.bat   如果没有在环境变量里面设置,那么每次启动的时候都需要调用这个。
emsdk_env.bat
cmake 安装

网上的一般例子都是直接用em++来做,这种方式只是用来编写做入门的测试例子,并没有什么代表性

mingw-64 安装

用于编译webassembly的c++程序,网上也有用nmake来编译的,但毕竟webassembly是类似于linux环境,使用mingw-64 编译更可靠(对于代码中有windows代码来说)

上面两个环境的安装最简单的办法就是安装qt程序,选择里面的自定义安装,勾选cmake 和 mingw64的版本 ,安装完成之后将其相关路径弄到环境变量中。如我安装的qt6.2版本

C++示例
  1. 编写cmake 工程文件
cmake_minimum_required(VERSION 3.4.1)

project(example CXX)

set(lib_name "example")

add_executable(${lib_name} main.cpp)

target_link_libraries(${lib_name})
# EXIT_RUNTIME=1 表示运行完main函数需要退出运行时。类似于em++ 编译时需要指定的参数
set_target_properties(${lib_name} PROPERTIES LINK_FLAGS "-s EXIT_RUNTIME=1")
#include 
#include 
int main(int argc, char *argv[])
{
	printf("hello world");
	return 0;
}
生成编译
#启动cmd 执行emsdk中的emsdk_env.bat之后,切换到当前CMakeLists.txt 
mkdir em
cd em 
emcmake cmake ..
emmake make 


在生成目录下生成了example.js 和example.wasm文件

编写html文件



   
   


//引用刚才生成的example.js文件



   

this is a example

运行启动
## 不能直接打开index.html
emrun --no_browser --port 8080 .

在浏览器中输入127.0.0.1:8080,按F12,打开开发者调试

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/883411.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号