File tree Expand file tree Collapse file tree 2 files changed +80
-0
lines changed Expand file tree Collapse file tree 2 files changed +80
-0
lines changed Original file line number Diff line number Diff line change 3636-  JavaScript 无法人为控制垃圾回收,而 WebAssembly 可以有效控制内存回收的时机;
3737
3838![ 在Web应用中嵌入WebAssembly] ( D:\GitHub\JavaScript\WebAssembly-编译目标\在Web应用中嵌入WebAssembly.png ) 
39+ 
40+ 
41+ 
42+ 
43+ 
44+ ## 实例:将C/C++编译为WebAssembly  
45+ 
46+ 
47+ 
48+ [ Emscripten] ( https://emscripten.org/ )  是一个开源项目,主要用于将C和C++代码编译为WebAssembly(Wasm),使其能够在Web浏览器、Node.js以及其他支持WebAssembly的运行时环境中运行。
49+ 
50+ 
51+ 
52+ ** [ 🚀下载安装:Download and install — Emscripten 4.0.15-git (dev) documentation] ( https://emscripten.org/docs/getting_started/downloads.html ) ** 
53+ 
54+ 
55+ 
56+ ** [ 🚀编译 C/C++ 为 WebAssembly - WebAssembly | MDN] ( https://developer.mozilla.org/zh-CN/docs/WebAssembly/Guides/C_to_Wasm ) ** 
57+ 
58+ 
59+ 
60+ 1.下载emsdk
61+ 
62+ ``` shell 
63+ #  emsdk克隆项目代码
64+ git clone https://github.com/juj/emsdk.git
65+ 
66+ #  进入emsdk目录
67+ cd  emsdk
68+ ``` 
69+ 
70+ 
71+ 
72+ 2.在Windows上安装
73+ 
74+ ``` shell 
75+ #  在 Linux 或者 Mac macOS 上
76+ ./emsdk install --build=Release sdk-incoming-64bit binaryen-master-64bit
77+ ./emsdk activate --global --build=Release sdk-incoming-64bit binaryen-master-64bit
78+ #  如果在你的 macos 上获得以下错误
79+ Error: No tool or SDK found by name ' sdk-incoming-64bit' 
80+ #  请执行
81+ ./emsdk install latest
82+ #  按照提示配置环境变量即可
83+ ./emsdk activate latest
84+ 
85+ #  在 Windows 上
86+ emsdk install --build=Release sdk-incoming-64bit binaryen-master-64bit
87+ emsdk activate --global --build=Release sdk-incoming-64bit binaryen-master-64bit
88+ ``` 
89+ 
90+ 
91+ 
92+ 3 .  C语言代码 hello.c
93+ 
94+  ``` c 
95+  #include  < stdio.h> 
96+  #include  < emscripten/emscripten.h> 
97+  
98+  int  main (int argc, char **  argv) {
99+  printf("Hello World\n");
100+  }
101+  
102+  #ifdef __ cplusplus
103+  extern "C" {
104+  #endif
105+  
106+  int EMSCRIPTEN_KEEPALIVE myFunction(int argc, char **  argv) {
107+  printf("我的函数已被调用\n");
108+  }
109+  
110+  #ifdef __ cplusplus
111+  }
112+  #endif
113+  ``` 
114+ 
115+   
116+ 
117+ 4.  
Original file line number Diff line number Diff line change 1+ Subproject commit 389a68bc35dcff7ebae4614e1615099dafda00d1
                         You can’t perform that action at this time. 
           
                  
0 commit comments