Giter Club home page Giter Club logo

fibonaccienarx's Introduction

WebAssembly with C++

C++ WebAssembly

Environment Setup

To compile this demo, you must install the following:

C++

Go to C and C++ Installation and follow the instructions.

Wasmer

Wasmer is an open-source runtime for executing WebAssembly on the Server.

Wasienv

Wasienv is a tool to compile different programming languages to WebAssembly, so you can run your programs on the Browser, or in the Server.

Wasmtime

You will find wasmtime at wasmtime.dev

C++ Code Snippet

We will create a Simple C++ Program that will return us the fibonacci sequence of an Integer Input.

Create a folder with the naming convention that you prefer. In My Case, I will be using "C++-to-WASM" as the name of my folder.

mkdir C++-to-WASM
cd C++-to-WASM

Create a file named FibonacciSequence.cpp, again you can choose any name for your file but make sure that you suffix it with the extension .cpp.

// Simple Program to calculate Fibonacci Sequence of an integer input
#include<iostream>
using namespace std;
int FibonacciSequence(int num) {
    if(num <= 1) {
        return num ;
    }
    return FibonacciSequence(num-1) + FibonacciSequence(num-2);
}
int main(){
    cout << "Enter the Number" << endl;
    int n ;
    cin  >> n ;
    
    cout << "Fibonacci Sequence term at " << n << "  " << "is " << FibonacciSequence(n) << endl;
}

Compiling the C++ Code

  1. Compiling Using g++
g++ FibonacciSequence.cpp

C++ Screenshot1

  1. Compile to WASM Binary using the following Command:

Since the Code has been written in C++, we need to figure out a way to generate a WebAssembly Binary. That's why we will be using wasienv in order to generate a .wasm binary from this cpp file.

When you have your cpp file created, you can execute wasic++

 wasic++ Fibonacci.cpp -o FibonacciBinary.wasm

Note that while executing this command, it might generate some warnings but you can ignore them.

  1. Now, you will have a new FibonacciBinary.wasm file ready in your Directory

  2. Executing it using WASM Runtime

wasmtime FibonacciBinary.wasm

C++ Screenshot2

fibonaccienarx's People

Contributors

deepansharora27 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.