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
CXX14=g++

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_CPPFLAGS = -I"../inst/include" -I"$(STANHEADERS_SRC)" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -DBOOST_MATH_OVERFLOW_ERROR_POLICY=errno_on_error $(STANC_FLAGS) -D_HAS_AUTO_PTR_ETC=0
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()")

CXX_STD = CXX17

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