WebAssembly编译
环境:centos 7.9
1、准备环境
sudo yum install docker
sudo systemctl start docker.service
sudo docker pull trzeci/emscripten:latest
2、编写C++源文件
//main.cpp
#include<stdio.h>
#include<string>
#include<cstring>
#ifdef __EMSCRIPTEN__
#include<emscripten.h>
#endif int main()
{
return 0;
}
extern "C"{ #ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
int add(int x,int y)
{
return x + y;
} #ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
int min(int x,int y)
{
return x -y;
} #ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
char* get(char* a)
{
return a;
} #ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
char* data(char* a)
{
*a = '\0';
char name[] = "hello from native code.";
strcat(a,name);
return a;
} #ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
int get_data(char* name,int lenth,char* onoutput)
{
char str[260]={0};
sprintf(str,"name:%s,lenth:%d,from native code.",name,lenth);
strcpy(onoutput,str);
return strlen(str);
}
}
3、启动docker,并将main.cc文件拷贝至docker中进行编译。
sudo docker run -ti 7634ecdacf21 /bin/bash
编译指令:
emcc main.cpp -o main.js -s EXTRA_EXPORTED_RUNTIME_METHODS=['ccall','cwrap','UTF8ToString']
编译后,产生如下main.js 和main.wasm文件。

4、测试
将main.js、main.wasm(两个文件存放目录一致)拷贝到web容器中,并编写main.html测试页面。
<html>
<head>
<meta charset="utf-8">
<title>WebAssemble Demo</title>
</head>
<body>
<button onclick="fun_add()">add</button>
<button onclick="fun_min()">min</button>
<button onclick="fun_get()">get</button>
<button onclick="fun_data()">data</button>
<button onclick="fun_get_data()">get_data</button>
<div id="log"></div>
<script>
function fun_add()
{
const add=Module.cwrap('add','number',['number,number']);
log.innerHTML=("add(12,5)的计算结果:" + add(12, 5));
}
function fun_min()
{
const min=Module.cwrap('min','number',['number','number']);
log.innerHTML=("min(12,5)的计算结果:" + min(12, 5));
}
function fun_get()
{
const get = Module.cwrap('get','string',['string']);
log.innerHTML=get('汉语-hanyu-demo!');
}
function fun_data()
{
const data = Module.cwrap('data','string',['string']);
log.innerHTML=data('');
}
function fun_get_data()
{
let onoutput="";
const pointer = Module._malloc(256);
const get_data = Module.ccall('get_data','number',['string','number','number'],['hello from web',129,pointer]);
onoutput = Module.UTF8ToString(pointer);
log.innerHTML=onoutput;
Module._free(pointer)
}
</script>
<script src="democpp.js"></script>
</body>
</html>
在浏览器中输入main.html页面请求地址,分别点击[add]、[min]、[ge]三个按钮进行测试,实验效果如下图所示:

WebAssembly编译的更多相关文章
- 对于WebAssembly编译出来的.wasm文件js如何调用
WebAssembly也叫浏览器字节码技术 这里就不过多的解释了网上很多介绍 主要是让大家知道在js里面如何调用执行它,我之前看WebAssemblyAPI时候反正是看得一脸懵逼 也是为了大家能更快的 ...
- 初探WebAssembly
1.前言 参加完2018年上海的QCon大会,想到了会议中来自Microsoft的朱力旻大佬讲的WebAssembly,感触颇深. 我之前完全没有了解过WebAssembly,之前没有了解的原因也很简 ...
- WebAssembly 介绍
http://blog.csdn.net/zhangzq86/article/details/61195685 WebAssembly 的出现是不是意味着 Javascript 要完? https:/ ...
- WebAssembly,Web的新时代
在浏览器之争中,Chrome凭借JavaScript的卓越性能取得了市场主导地位,然而由于javascript的无类型特性,导致其运行时消耗大量的性能做为代价,这也是JavaScript的瓶颈之一.W ...
- 纵论WebAssembly,JS在性能逆境下召唤强援
webassembly的作用 webassembly是一种底层的二进制数据格式和一套可以操作这种数据的JS接口的统称.我们可以认为webassembly的范畴里包含两部分 wasm: 一种体积小.加载 ...
- 在Windows10搭建WebAssembly开发环境
最近研究WebAssembly技术,准备用WebAssembly编译C/C++代码供前端调用.网上看了很多文章,收获很大,现在就遇到的问题做一个记录. 官网关于windows开发环境搭建基本上几句话, ...
- shell 中长命令的换行处理
考察下面的脚本: emcc -o ./dist/test.html --shell-file ./tmp.html --source-map-base dist -O3 -g4 --source-ma ...
- Linux 一条长命令占用多行
前言 考察下面的脚本: ? 1 emcc -o ./dist/test.html --shell-file ./tmp.html --source-map-base dist -O3 -g4 --so ...
- Blazor带我重玩前端(一)
写在前面 曾经我和前端朋友聊天的时候,我说我希望有一天可以用C#写前端,不过当时更多的是美好的想象,而现在这一切正变得真实…… 什么是Blazor 我们知道浏览器可以正确解释并执行JavaScript ...
随机推荐
- [源码解析] PyTorch 分布式之弹性训练(4)---Rendezvous 架构和逻辑
[源码解析] PyTorch 分布式之弹性训练(4)---Rendezvous 架构和逻辑 目录 [源码解析] PyTorch 分布式之弹性训练(4)---Rendezvous 架构和逻辑 0x00 ...
- 应用程序开发 WebApp NativeApp 微信小程序
Web Native App 微信小程序 WebApp是指基于Web的系统和应用,其作用是向广大的最终用户发布一组复杂的内容和功能.webapp 框架是一种简单的与WSGI兼容的网络应用程序框 ...
- 第二十八个知识点:什么是公钥密码学的IND-CCA安全定义?
第二十八个知识点:什么是公钥密码学的IND-CCA安全定义? 我们将在这篇博客中讨论公钥加密的IND-CCA安全. IND-CCA安全代表选择明文的不可伪造性.这样的安全方案的思想就是给定一个密文,攻 ...
- Log4自定义Appender介绍
最初想要在执行一段业务逻辑的时候调用一个外部接口记录审计信息,一直找不到一个比较优雅的方式,经过讨论觉得log4j自定义的appender或许可以实现此功能.后来就了解了一下log4j的这部分. Ap ...
- 编写Java程序,使用面向接口编程模拟不同动物的吼叫声
返回本章节 返回作业目录 需求说明: 使用面向接口编程模拟不同动物的吼叫声 实现思路: 使用面向接口编程模拟不同动物吼叫声的实现思路: 定义发声接口Voice,在其中定义抽象吼叫方法sing(). 分 ...
- 啥是Gossip协议?
你好呀,我是歪歪. 元旦的时候我看到一个特别离谱的谣言啊,具体是什么内容我就不说了,我怕脏了大家的眼睛. 但是,我看到一个群里传的那叫一个绘声绘色,大家讨论的风生水起的,仿佛大家就在现场似的. 这事吧 ...
- sqlserver - 判断字段是否是纯数字
PATINDEX('%[^0-9|.|-|+]%',w.waterMeterNo)=0 如 SELECT w.* FROM [dbo].[waterMeterInfo] w where isnull( ...
- Centos6.8安装并配置VNC
一般服务器都会在IDC或云端,为了可以看到服务器的图形化界面,需要安装配置VNC,本例为Centos6.8上安装配置VNC. [root@hostname ~]#yum install -y tige ...
- centos7 配置rm命令失效。
之前写过一篇alias别名的方法,但这个方法配置rm命令在重启后就无效了.而且重启后得输入source alias_test.sh.才可以 centos7 alias别名永久生效 原因是:root用户 ...
- 一站式搭建 GitHub Pages 博客 (一)
本文将详细讲解如何快速搭建 GitHub Pages 博客页面 关于博客主题,博客信息更改,上传文章等将会在 一站式搭建 GitHub Pages 博客 (二) 中进行详细讲解 准备阶段 注册 Git ...