# Building a Rstan package with custom c++ code

**URL:** <https://discourse.mc-stan.org/t/building-a-rstan-package-with-custom-c-code/11126>\
**Category:** RStan\
**Created:** [September 26, 2019, 4:12am UTC](https://discourse.mc-stan.org/t/building-a-rstan-package-with-custom-c-code/11126 "2019-09-26T04:12:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stemangiola](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/stemangiola/32/260_2.png) [@stemangiola](https://discourse.mc-stan.org/u/stemangiola)\
**Post date:** [September 26, 2019, 4:12am UTC](https://discourse.mc-stan.org/t/building-a-rstan-package-with-custom-c-code/11126/1 "2019-09-26T04:12:51Z")

</div>

I looked at several resources but it is not clear to me how can I include the c++ file

aaa.cpp

```
#include <stdio.h>
#include <stdlib.h>

int main(){
	printf(" %d \n ", 3);
}

```

in a stan model within an R package. In particular I have two questions:

1. Where should I put the file aaa.cpp
2. how should I link the file (e.g., #include \<path?.cpp\>)

Thanks a lot

---

<div class="post-metadata">

**Author:** ![mcol](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mcol/32/5598_2.png) [@mcol](https://discourse.mc-stan.org/u/mcol)\
**Post date:** [September 26, 2019, 4:26am UTC](https://discourse.mc-stan.org/t/building-a-rstan-package-with-custom-c-code/11126/2 "2019-09-26T04:26:44Z")

</div>

The easiest way is to use Rcpp. There are tons of resources out there, but this is a good start: [http://dirk.eddelbuettel.com/code/rcpp/Rcpp-package.pdf](http://dirk.eddelbuettel.com/code/rcpp/Rcpp-package.pdf)

---

<div class="post-metadata">

**Author:** ![mcol](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mcol/32/5598_2.png) [@mcol](https://discourse.mc-stan.org/u/mcol)\
**Post date:** [September 26, 2019, 4:31am UTC](https://discourse.mc-stan.org/t/building-a-rstan-package-with-custom-c-code/11126/3 "2019-09-26T04:31:53Z")

</div>

Or I should read better! If you put your c++ code in a subdirectory of your stan models, you can include them using

```
functions {
#include subdir/cppfile
}

```

Note no spaces before the hash symbol, Stan is very picky about these things!

---

<div class="post-metadata">

**Author:** ![stemangiola](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/stemangiola/32/260_2.png) [@stemangiola](https://discourse.mc-stan.org/u/stemangiola)\
**Post date:** [September 26, 2019, 4:34am UTC](https://discourse.mc-stan.org/t/building-a-rstan-package-with-custom-c-code/11126/4 "2019-09-26T04:34:11Z")

</div>

Thanks,

just to confirm, this source c++ code will get compiled at the moment of R package installation together with the \*.stan code

My worry is that hoe the R installer knows which files to compile?

---

<div class="post-metadata">

**Author:** ![mcol](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mcol/32/5598_2.png) [@mcol](https://discourse.mc-stan.org/u/mcol)\
**Post date:** [September 26, 2019, 4:39am UTC](https://discourse.mc-stan.org/t/building-a-rstan-package-with-custom-c-code/11126/5 "2019-09-26T04:39:57Z")

</div>

If you use rstantools, it will set everything up for you, then the R package installation process will work as usual. Note that in general you can only write functions, not a main().
