Reputation: 9373
I have written some basic c++ programs in one of my classes for school. I was wondering if it was possible to somehow virtually run the program in a broswer. I would like to post the program to my website. Once its posted, a person could access the program, run the program, and, interact with the program. I'm not trying to write C++ for my website, it would be more for an interactive portfolio.
Is this possible?
Upvotes: 20
Views: 35208
Reputation: 31810
You can use Emscripten to compile C++ to Javascript. Emscripten can compile LLVM bitcode to Javascript. Some demos of Emscripten can be found here, including a raytracer and a text-to-speech engine that was compiled from C++ to Javascript.
To run x86 binaries in a web browser, you could also use an emulator such as v86. This is one possible way to compile and run C++ programs in a browser.
Alternatively, you can compile C++ programs into GLSL shaders and run them in a browser using WebGL.
Upvotes: 2
Reputation: 3204
Yes it's possible and I can guarantee if you follow this tutorial you will be shocked about how super easy is to use to C++ in a modern browser with Cheerp
Here is a simple example of how to combine you cpp file in js browser. For example, suppose that you have a project in which you need to sort a big array, and you want to replace the slow Array.sort()
with a faster version. Instead of writing your own sorting function, you can use std::sort()
from the C++ STL:
#include <cheerp/client.h>
#include <algorithm>
[[cheerp::jsexport]]
extern "C" void sort(client::Int32Array* a)
{
int* begin = &(*a)[0];
int* end = begin + (int)a->get_length();
std::sort(begin, end);
}
You can compile the above code with /opt/cheerp/bin/clang++ -target cheerp sort.cpp -o sort.js -cheerp-make-module=commonjs
and put the resulting sort.js in your project source folder. Than, using it is as easy as:
let cheerplib = require("./sort");
cheerplib.then((cl) => {
let arr = new Int32Array([4, 2, 3, 1]);
cl.sort(arr);
console.log("Sorted with std::sort: " + arr);
});
Upvotes: 0
Reputation: 9865
One of the best sites for running C++ and other multiple languages online is Repl.it
This example: repl.it/@abranhe/stackoverflow
#include <iostream>
int main() {
std::cout << "Hello Stackoverflow\n";
return 0;
}
One of the biggest pros it has is that you can work with multiple files, working with header (header.h
) files etc. None of the below websites provide this option:
I really recommend it! You will love it!
Upvotes: 1
Reputation: 1481
Also wanted to add Google Colab here as an option:
Cell 1:
%%writefile hello.cpp
#include <iostream>
int main(int argc, char** argv) {
std::cout << "Hello, Stack Overflow!" << std::endl;
return 0;
}
Cell 2:
%%script bash
g++ hello.cpp -o test
ls -laX
./test
Upvotes: 0
Reputation: 17997
Another solution (codepad like) would be to use https://ideone.com/ which seems much nicer to use than codepad, more user-friendly, but does the same:
Allow you to write C++ (60 languages possibles) directly from the browser and compile it and render result in the browser (I tried using printf
and it worked fine). Possibility of forking source code.
Upvotes: 2
Reputation: 32923
Use codepad, a website which lets you compile and share code online.
#include <iostream>
int main(int argc, char** argv) {
std::cout << "Hello, Stack Overflow!" << std::endl;
return 0;
}
Upvotes: 11
Reputation: 48775
Google chrome supports this: http://www.readwriteweb.com/cloud/2011/08/google-officially-announces-cc.php
But it's by no means "mainstream" or standards-based.
Upvotes: 2
Reputation: 24403
There is also Google Native Client SDK that allows C++ code to run in browser. Microsoft Active X is also a viable option. I am only saying it is possible not recommended.
Upvotes: 5
Reputation: 354476
I see two options, but both very overkill:
The sensible option is to just give people a way to view and download the source code, I guess.
Upvotes: 2
Reputation: 14477
You can only run the program on your server, not on the client's machine.
At least not without downloading and manually executing it. Anything else would be an open door for malware...
Upvotes: 3