首页 > 编程语言 >emsdk安装和编译2个C++基础示例

emsdk安装和编译2个C++基础示例

时间:2024-08-05 17:39:09浏览次数:8  
标签:示例 C++ 编译 output cpp input hello2 emsdk

参考地址:Download and install — Emscripten 3.1.65-git (dev) documentation

 

环境:

ubuntu 24.04 LTS

gcc (Ubuntu 13.2.0-23ubuntu4) 13.2.0

g++ (Ubuntu 13.2.0-23ubuntu4) 13.2.0

cmake version 3.28.3

 

First check the Platform-specific notes below and install any prerequisites.

The core Emscripten SDK (emsdk) driver is a Python script. You can get it for the first time with

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

# Enter that directory
cd emsdk

 

Run the following emsdk commands to get the latest tools from GitHub and set them as active:

# 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
source ./emsdk_env.sh

Python is not provided by emsdk. The user is expected to install this beforehand with the system package manager:

# Install Python
sudo apt-get install python3

# Install CMake (optional, only needed for tests and building Binaryen or LLVM)
sudo apt-get install cmake

 

来2个示例:

eg1:

/// /input/hello1.cpp

#include <iostream>
using namespace std;

int main(){

  std::cout<<"Hello emsdk!"<<std::endl;

  return 0;
}


// step1: 编译C++工程
// emcc ./input/hello1.cpp -o ./output/hello1/hello1.html

// step2: 运行web
// emrun --no_browser  --port 8081  ./output/hello1/

// step3: 浏览器查看
// localhost:8081/hello1.html

eg2:

/// input/hello2.cpp

#include <iostream>  
  
extern "C" {  
    void say_hello2() {  
        std::cout << "Hello, Emscripten! 20240805" << std::endl;  
    }  
}  
  
int main() {  
    // 注意:在WebAssembly中,main函数可能不会被直接调用,  
    // 但我们可以从JavaScript中调用say_hello。  
    // 这里只是为了演示如何写C++代码。  
    say_hello2();  
    return 0;  
}


// step1: 编译C++工程,这里导出了一个函数
// emcc  -s WASM=1 -o ./output/hello2/hello2.js  ./input/hello2.cpp -s EXPORTED_FUNCTIONS='["_say_hello2"]'   -s EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]' 

// step2: 运行web
// emrun --no_browser  --port 8082  ./output/hello2/

// step3: 浏览器查看
// localhost:8082/index.html
// output/hello2/index.html

<!DOCTYPE html>  
<html>  
<head>  
    <title>Emscripten Example</title>  
</head>  
<body>  
    <h1>Emscripten C++ Example</h1>  
    <button onclick="Module.ccall('say_hello2', null, [], [])">Say Hello</button>  
    <script src="./hello2.js"></script>  
</body>  
</html>

 

 

 

注意这里的目录结构,

emsdk,  input, output 3个目录是同级的。

emsdk/xxx

input/ 
  hello1.cpp
  hello2.cpp

output/
  hello1/
  hello2 /
      index.html
     

编写3个文件, input/hello1.cpp,  input/hello2.cpp,  output/hello2/index.html 。完成这3个文件的编写。

在 emsdk 文件夹所在目录,执行一些  工程编译的命令,编译C++代码。可以完成这2个工程。

## 第一个工程的编译命令
emcc ./input/hello1.cpp -o ./output/hello1/hello1.html


## 第2个工程的编译命令
emcc  -s WASM=1 -o ./output/hello2/hello2.js  ./input/hello2.cpp -s EXPORTED_FUNCTIONS='["_say_hello2"]'   -s EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]' 

实际使用过程中,一个工程,一个工程来编译。完成了eg1, 再去完成eg2.注意

如果执行编译命令的时候,提示emcc找不到,那应该是emsdk文件夹下面的 环境变量脚本没有执行。

参考刚才安装步骤,把环境变量脚本 用source 命令执行一下。

还有,看看这的若干编译命令,要在  emsdk, input, output,这3个文件夹 同级目录执行编译命令。否则直接执行脚本,会找不到文件。

如果你看懂了这里脚本的路径,你可以根据自己的需要改写脚本路径。

 

标签:示例,C++,编译,output,cpp,input,hello2,emsdk
From: https://www.cnblogs.com/music-liang/p/18343719

相关文章

  • leetcode200. 岛屿数量C++题解,精美图例和流程图,一题带你弄懂图的dfs遍历算法
    leetcode200.岛屿数量给你一个由‘1’(陆地)和‘0’(水)组成的的二维网格,请你计算网格中岛屿的数量。岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。此外,你可以假设该网格的四条边均被水包围。示例1:输入:grid=[[“1”,“1”,“1”,......
  • v-md-editor示例
    pnpmcreatefarm@latest#选择vue3cdprojectpnpmi@kangc/v-md-editor@next修改index.ts:import{createApp}from'vue';//import'./style.css';importAppfrom'./App.vue';importVueMarkdownEditorfrom'@kangc/v-md-ed......
  • C/C++ 面试常见问题
    1.封装、继承和多态是什么?封装:将具体实现过程和数据封装成一个函数,只能通过接口访问,降低耦合性,使类成为一个具有内部数据自我隐藏能力且功能独立的软件模块。封装能够通过提供公共接口访问、不让类外的程序直接访问或修改来防止类中代码被破坏。继承:子类继承父类的行为和特征,复......
  • Kotlin 布尔值教程:深入理解与应用示例
    Kotlin布尔值在编程中,您经常需要一种只能有两个值的数据类型,例如:是/否开/关真/假为此,Kotlin有一种布尔数据类型,可以取true或false值。布尔值布尔类型可以用Boolean关键字声明,并且只能取true或false值:示例valisKotlinFun:Boolean=truevalisFish......
  • vue2 - 最新详细实现高德地图绘制动态热力图详细教程,在某区域或城市地图上做“热力图
    效果图在vue2、nuxt2项目开发中,详解引入使用高德地图接收热力图数据并渲染“热力图”效果功能,在地图上的某个区域或某个城市(可多个)、省份等自由绘制对应的热力图层,各城市地区同时加载渲染热力流量区域用以对比,根据不同的颜色代表人口密度、客流量、旅游人数、交通流量......
  • Kotlin 布尔值教程:深入理解与应用示例
    Kotlin布尔值在编程中,您经常需要一种只能有两个值的数据类型,例如:是/否开/关真/假为此,Kotlin有一种布尔数据类型,可以取true或false值。布尔值布尔类型可以用Boolean关键字声明,并且只能取true或false值:示例valisKotlinFun:Boolean=truevalisFi......
  • C++ 中,static 和非 static
    在C++中,static和非static的变量在作用域、生命周期和初始化方面有一些重要的区别。下面详细解释这两种变量的不同之处:非static变量inti0=123;作用域:变量i0的作用域是它所在的代码块或函数。它只能在定义它的代码块内访问。生命周期:每次进入代码块时,变量i0会被创......
  • [C++] 简单解析http请求
    #include<iostream>#include<string>#include<map>#include<vector>#include<regex>classHttpRequest{public:enumMethod{GET,POST,UNKNOWN};enumError{SUCCESS,......
  • c++递归
    这是我发的第一篇讲解类型的文章主要是报的班那边讲到了个很有趣的东西到时候会给些案例本期直接把花絮挂在最后面_____________________________________________________________________________c++里有两种函数一种是可以看成数据的(这种定义函数的类型有longlong,......
  • c++中的sort
    前言Hello,大家好啊,我是文宇。正文sort函数是C++标准库提供的用于对数组或容器中的元素进行排序的函数。通过使用快速排序或其它高效的排序算法,sort函数能够以非常高效的方式对元素进行排序。sort函数用法灵活多样,可以对不同类型的元素进行排序,并且可以通过自定义比较函数或......