Has anyone used emscripten?

I've used other virtual c++ compilers before, mostly ones to be able to write c++ on android. I know there's always tradeoffs when doing something goofy to a language or platform so they can act like something they're not.

So have you gone into any depth in emscripten, and are there any pratfalls that might trip up a newbieg? Does heredity/polymorphism work as expected, is c++11-17 supported, etc.

Can you recommend a free webost that can host the resulting html/js/wasm code? Would WordPress work?
Last edited on
I've used it. Emscripten uses clang. It compiles everything to LLVM bitcode, then to WebASM or Javascript. Everything regarding language features should work fine, though there may be exceptions that I haven't run into. There are a few differences, like if you use OpenGL, you have to use a subset of OpenGL 2.0 ES. I'm not sure if OpenGL ES 3.0 is supported yet. You also have to recompile every external library you use, but they do offer ports. Resources on how to do this are decent, but expect to get your hands dirty.
Thank you JagerDesu, I've finally got time to test emscripten out. I didn't delve too deep yet, but here's what I've experienced so far.

It was not too difficult to set it up; download the source file from github, follow instructions to install and update. Then I found the test files in emsdk/upstream/emscripten/test, and it already has SDL/SDL2/ttf/image/mixer/gfx code examples.

I still don't have a webhost, so I can't test out preloading files yet and I'm compiling to a single file for the same reason. SDL libs are downloaded automatically the first time I test a library, and that is downloaded to a cache folder inside the git project folders, so it doesn't need to re-download after that.

I compiled the sdl2_ttf.c file after learning about adding a resource folder "res" with my chosen font inside into that single file.
em++ sdl2_ttf.c -s USE_SDL=2 -s USE_SDL_TTF=2 -s SINGLE_FILE --embed-file res -o build/hello.html
The resulting html file is 3.2Mb, so a bit on the large size compared to 18Kb compiled by g++, but I understand that all of the SDL libraries used are also built into this file, probably also with the standard C/C++ template libs and more. Worth it so far for a build once run anywhere capability.

I compiled this on a linux machine, then tested the result on a 10 year old computer without SDL installed, and it ran just fine, so yes, it is properly packaged and sand-boxed.

The documentation is excellent; presented as a step-by-step tutorial which is easy to read compared to some other docs https://emscripten.org/docs/introducing_emscripten/index.html

I'm going to have to stress test things to try and figure out the limits to this system, but I'm already very impressed on day 1.


Edit: Moved from lounge to general C++ forum.


Last edited on
OK, so saving files feels a bit hackish on emscripten.
A lot can be created with the default filesystem they have set up (temporary file-saving that exists until the page is refreshed or closed, then anything saved is lost).
There's some extra steps to get a more permanent filesystem <-(which looks to save data as cache and-or cookies, though I'm not certain of this), and that also seams to have some harsh limits. Not a game-ender, but certainly dampens some of my enthusiasm. I blame most of this on the sandboxing, a hard trade-off for gains in security.

... Thinking about how many games are set up now, a large portion of free games make money off of advertising, those kinds of games would benefit from creating good content that makes the user reluctant to close the tab. Until you remember that add-blockers exist, so many players will never see said adds. Sheesh.

I still think it's an amazing tool.
Last edited on
Topic archived. No new replies allowed.