Compiling Stan code for R package

I am building an R package with Stan, and the .o files in my compiled C++ code are extremely large (i.e. can’t add to Github).

A previous post talked about mitigating this problem by adding the following to the src/Makevars file:

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

I’m wondering where in the file this should be added? And since this file is generated by rstantools, how do we edit it?

Below is my src/Makecars file.

STANHEADERS_SRC = $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "message()" -e "cat(system.file('include', 'src', package = 'StanHeaders', mustWork = TRUE))" -e "message()" | grep "StanHeaders")

STANC_FLAGS = $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "cat(ifelse(utils::packageVersion('rstan') >= '2.26', '-DUSE_STANC3',''))")
PKG_CXXFLAGS = $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "RcppParallel::CxxFlags()") $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "StanHeaders:::CxxFlags()")
PKG_LIBS = $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "RcppParallel::RcppParallelLibs()") $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "StanHeaders:::LdFlags()")


Yeah those files can get big, but you don’t actually need to keep them on GitHub. I would delete them (if already on GitHub) and then add them to the .gitignore file in the repository. That way you can just keep those locally on your computer for convenience.

Sounds great, thank you!

1 Like