How to specify include path when compiling stan models?

Hi,

I use several different models which all share the same data block format. I would therefore like to put this data block into a separate file “standard_data_block.stan” and then use the #include mechanism mentioned in the manual. So at the top of each model I would place
#include “standard_data_block.stan”

However for this to work I need to specify an include-path. The manual only alludes to some mechanism for doing this but doesn’t specify the exact method. After looking through some of the source code, I think this is done by specifying --include-paths as a STANCFLAG when running make. However I can’t get this working properly, the compiler still complains that it couldn’t find the right include file.

This is the command I’m running, does it look correct? The model is bias_sens.stan, and in the same parent directory I have the data_block file.

peter at pzh in ~/cmdstan-2.17.1
$ make STANCFLAGS=“–include-paths=/Users/peter/Desktop/stan2AFC/models” /Users/peter/Desktop/stan2AFC/models/bias_sens

— Translating Stan model to C++ code —
bin/stanc --include-paths=/Users/peter/Desktop/stan2AFC/models /Users/peter/Desktop/stan2AFC/models/bias_sens.stan --o=/Users/peter/Desktop/stan2AFC/models/bias_sens.hpp
Model name=bias_sens_model
Input file=/Users/peter/Desktop/stan2AFC/models/bias_sens.stan
Output file=/Users/peter/Desktop/stan2AFC/models/bias_sens.hpp

could not find include file
make: *** [/Users/peter/Desktop/stan2AFC/models/bias_sens.hpp] Error 255

If it’s not correct, what is the command I should be using to specify an include path for STANC? Thank you!

1 Like

I think that is more for including C++ headers that define undefined user-defined functions. To insert some Stan code, it should just work but it is very difficult. Make sure that the #include statement is flush left. Then make sure there is no quotes surrounding the filename. Then make sure there are no spaces. Then make sure there is nothing to the right of the filename, not even whitespace or comments.

This doesn’t work for me. For

#include foo.stan
data{
/*...

in ~/some_work_directory/bar.stan to compile I have to put foo.stan at cmdstan's root folder. It won’t work if foo.stan is at the same work folder with bar.stan.

It is tricky. I think you currently need to use a full path, such as

#include /home/you/some_work_directory/bar.stan
data {
...

The compile function in the Stan library has an include_paths argument that allows you to specify a path so that the #include statements can be relative to that, but stanc in CmdStan does not accept it.

1 Like

Thanks.

Stan reference 2.17 is misleading here

The Stan interfaces provide a mechanism for specifying a sequence of system paths in which to search for include files.

There is a PR to improve things

There are a bunch of things including this that have been implemented at the C++ level but not in the interfaces. I’ll open an issue for this one. The PR that @bgoodri is pointing to is just for fixing the underlying C++ to make the location of the include a little more flexible and to allow paths without final /.

I added a CmdStan issue to address this.

1 Like

I’ll fix it to say “may provide” until the interfaces catch up. I added an issue comment so I won’t forget.