WebAssembly contains instructions in binary code for a virtual machchine that can be run on the web browser. I use emscripten tools to try out WebAssembly.
Examples
- Create C++ code
hello.cpp
#include<iostream>
int main() {
std::cout << "Hello World!\n";
return 0;
}
-
Compile the C++ file with
emcc hello.cpp -o hello.html
. It generateswasm
, JavaScript and HTML files.
Here is one more example showing a colored cube on a Canvas using SDL. I don't know how much of SDL is supported yet.
Emscripten
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
Update tools (on Linux):
git pull
./emsdk install latest
./emsdk activate latest
source ./emsdk_env.sh
Input Files
Using input files, here the example main.cpp
uses ifstream
:
#include <iostream>
#include <fstream>
int main() {
std::ifstream infile("main.cpp");
std::string line;
while(std::getline(infile, line)) {
std::cout << line << std::endl;
}
return 0;
}
Compile with --preload-file
:
emcc main.cpp -o index.html --preload-file main.cpp