.so file size when building an R package that uses compiled Stan code


@bgoodri has prompted me to build a separate R package for my R functions that use rstan. I followed the instructions here: https://mc-stan.org/rstantools/articles/minimal-rstan-package.html except that I did not run compile_dll. I’m running Ubuntu 20.04 with rstantools 2.1.1. Everthing has worked well and I can run the new package. But upon installation of the package, the libs/packagename.so file is 52MB in size, even with only one Stan module being compiled. The entirety of rstanarm has an .so file that is only about 9MB with many Stan programs included.

I am using only defaults when I run rstan_create_package().

How do I get a much smaller .so file?

That usually means that it was compiled with debug symbols. If you add -g0 to your ~/.R/Makevars file, it should be fine. The rstanarm.so is only 18MB and it has several more compiled Stan programs.

Thanks Ben. So this is what I’m trying for !/.R/Makevars

CXX14FLAGS=-O3 -march=native -mtune=native -fPIC -g0

That is fine. R CMD check will emit a warning message on your machine about the -march=native -mtune=native flags, but the .tar.gz file that it creates will be fine on winbuilder / CRAN.

1 Like

Thanks much. Down to 2MB now. And thanks for putting me onto doing compilation of Stan code during package buiild. This will make it much easier on users while avoiding the serialization problem.

Yeah, everyone should do that, although not everyone does. It makes things much easier for Mac / Windows users.

1 Like

I hope that CRAN uses the appropriate compiler flags when they produce binary packages.

They do when they create binaries, although CRAN’s testing framework utilizes flags that include debug info so you may see the huge sizes under


but that is not problematic for CRAN.