Translating stan model to C++ code (how to inline "everything") - and related efforts using em++ to get Stan running in the browser


I just did this :

and got something like this :

Is it possible to create a “FAT” bernoulli.hpp which is completely self contained ? Let’s call it bernoulliFAT.cpp.

Something like : gcc bernoulliFAT.cpp would produce the same binary output as this compiling / llinking command :


So, there are two commands that lead to the bernoulli executable:


-I src 
-I stan/src 
-I stan/lib/stan_math/ 
-I stan/lib/stan_math/lib/eigen_3.3.3 
-I stan/lib/stan_math/lib/boost_1.69.0 
-I stan/lib/stan_math/lib/sundials_4.1.0/include    
-x c++ 
-o examples/bernoulli/bernoulli.o 

Linking :

-pthread -Wno-sign-compare    
-I src 
-I stan/src 
-I stan/lib/stan_math/ 
-I stan/lib/stan_math/lib/eigen_3.3.3 
-I stan/lib/stan_math/lib/boost_1.69.0 
-I stan/lib/stan_math/lib/sundials_4.1.0/include   
-o examples/bernoulli/bernoulli

So, instead of the above ^ two commands. I would like to produce a BIG BIG BIG C++ file (called bernoulliFAT.cpp) that contains EVERYTHING as C++ source code ONLY and which can be compile+linked by a single command:

g++ bernoully.fat -o bernoiulli_from_fat

and then bernoiully_from_fat will be the same binary as examples/bernoulli/bernoulli

and will output this when executed by itself :

and if I type examples/bernoulli/bernoulli_from_fat sample data file=examples/bernoulli/

then it outputs something like :

Is there an easy way to create such an inlined file ?

I was reading this :

but … its not simple…

also, it seems that I can make a .js (or LLVM) runnable stan library (AFAIU) :

if I set this into the local file :

it even gives me a stanc file !

so, i have a stanc compiled by em++

and one compiled by g++ :

and, this also works :

but I just have not idea what to do with that bin/stanc

now, I am trying this :

which gave me this :

but somehow the resulting .js file cannot be run with node

it errors out…

anyway … i might come back to this at some point … still does not work :(

if you want to play around with this, here is the docker image that you can pull :



In general taking some subset of generated C++ code and putting in into a separate text file, and trying to compile it with a C++ compiler without proper headers, includes, and generally, well, making sure the file compiles, won’t compile.

If you really need to use a distribution outside of Stan you might consider writing your own sampler in C++ outside of Stan. Just copying and pasting a a subset of a generated C++ file won’t compile. Because it has so many dependencies in the Stan math library.

The Stan devs here mostly to answer questions regarding use of Stan modeling language, Stan development, Stan, modeling, etc.

This is more of a general C++ software dev question that could be answered by searching around elsewhere.

I agree, I had that feeling myself :).

However, the preliminary results are pretty encouraging.

I think I need to ask this on the em++ forum, or something similar.

1 Like

idk anything about emcc, but is their guide wrong that it’s not a drop in replacement for gcc/clang?

if you wanna hack it, pull this docker :

docker pull jhegedus42/welcome_the_lion_on_your_path

here is how to use the image

it’s quite mess…

Is this project going anywhere? There hasn’t been any updates in the first repo for two months and now you’ve made a new repo that contains…what, a Docker tutorial?

Back when you made the first thread I was interested enough to try my hand at this. I made a bit of progress but eventually gave up for some dumb reason. Not really interested in continuing but I did finally get around to cleaning up my code and put it on GitHub. While you can fit models either on the server or in the browser I don’t think it’s possible to compile them in the browser.

1 Like

Nice ! Interesting. Yeah, thats a good questions. Maybe it is not possible to compile them in the browser.

I put all my code etc… into that docker… so if somebody wants to try it … then it should work out of the box, on any machine… the new repo only talks about how to use the docker image.

Docker is nice because it is machine / environment independent. Whatever I do, can be reproduced out of the box. It’s basically 2 lines on any docker installed machine and then you have the same environment I have, no need to install anything … so the github repo only contains the scripts I use to start/stop the docker image… it’s work in progress… not perfect but at least if somebody wants to give it a try does not have to install 100s of dependencies… compilers whatnot… he/she will have exactly the same setup/version everything that I have… so there is not such thing is “ohhh but it works on my machine”.

Docker takes out the pain of setting up the environment.

I mean, just to set up my environment on osx took me a few hours.

Now with this docker image, on any machine , where there is docker, it takes 2 commands.

Hence the docker…

I was somehow under the impression that this is somehow obvious - so I did not write a novel about it - in that repo.

Basically, whatever this new thread has, was made inside that docker image.

If anybody wants to reproduce it then there are two lines needed to start the image.

  1. pull it
  2. start it

I just checked it, this is how you start the docker image :

Then you have everything to reproduce which I posted in this thread.

In other words, to have a fully functional setup for compiling Stan to JS (hopefully), you need to type 5 lines of code:

$ git clone
$ cd wellcome_the_lion_on_your_path
$ cd docker
$ docker pull jhegedus42/welcome_the_lion_on_your_path:0010
$ ./

After this, you have an environment in which you can reproduce what I posted in this thread, and you don’t have to spend 1000 hours of setting up the right flags and whatnot for the right compilers, the right versions, bla bla bla …

Just type 5 lines of code and you can hack away… on playing around on how to compile Stan to JS.

I got this far…

I might come back to it later, but if anybody wants, now it only takes 5 lines of code to start to play with it… I hope this helps a bit.

I think Stan in the browser has some nice potential. Just imagine a stan.js file that you can include into a .html page and use Stan from any JS code … I think that is very awesome idea.