# Include a C++ function in a Stan code

**URL:** <https://discourse.julialang.org/t/include-a-c-function-in-a-stan-code/40782>\
**Category:** General Usage\
**Tags:** stan\
**Created:** [June 5, 2020, 1:27am UTC](https://discourse.julialang.org/t/include-a-c-function-in-a-stan-code/40782 "2020-06-05T01:27:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![99dB](https://avatars.discourse-cdn.com/v4/letter/9/838e76/32.png) [@99dB](https://discourse.julialang.org/u/99dB)\
**Post date:** [June 5, 2020, 1:27am UTC](https://discourse.julialang.org/t/include-a-c-function-in-a-stan-code/40782/1 "2020-06-05T01:27:35Z")

</div>

Dear all,  
I need to include a C++ function in a stan code but I don’t know how I can do it.

The function is remainder and then I include the associated library but I get this error:

```julia
SystemError: opening file "<cmath>": No such file or directory

```

Here a draft:

```julia
stancode = "functions {
   real[] P_growth(real t, // time
                   real[] x, // state
                   real[] theta, // parameters
                   real[] x_r, // environmental data
                   int[] x_i){
    real gamma = theta[1];
    real lambda = theta[2];

    real growth = gamma*x[1];

    #include <cmath>
    if (remainder(t,24) < 14) {
        real loss = lambda*x[1]*x[1];
    } else {
        real loss = 0;
    }
    
    
    return {growth - loss};
  }
}
data {
    int<lower = 0> N; // num obs
    real t_obs[N]; // obs times
    real<lower = 0> y[N]; // observed variable at measurement times
    real sigma;
}
parameters {
    real<lower=0,upper=1> theta[2]; // parameters
    real<lower=0> x0[1];
}
transformed parameters {
    real x[N,1] = integrate_ode_rk45(P_growth, x0, -1, t_obs, theta,
                                      rep_array(0.0, 0), rep_array(0, 0),
                                      1e-6, 1e-5, 1e3);
}
model {
    //theta[1] ~ normal(0.1, 2);
    //theta[2] ~ normal(0.1, 2);
    x0 ~ normal(1.0, 10);
    y[1:N] ~ normal(x[1:N,1], sigma); // obs
}";

```

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![goedman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goedman/32/217_2.png) [@goedman](https://discourse.julialang.org/u/goedman)\
**Post date:** [June 5, 2020, 3:41am UTC](https://discourse.julialang.org/t/include-a-c-function-in-a-stan-code/40782/2 "2020-06-05T03:41:02Z")

</div>

Not sure if this is helpful: [Interfacing with External C++ Code](https://cran.r-project.org/web/packages/rstan/vignettes/external.html)

If that doesn’t help probably our best bet is Stan discourse or to ask Bob C directly.

---

<div class="post-metadata">

**Author:** ![99dB](https://avatars.discourse-cdn.com/v4/letter/9/838e76/32.png) [@99dB](https://discourse.julialang.org/u/99dB)\
**Post date:** [June 5, 2020, 4:11am UTC](https://discourse.julialang.org/t/include-a-c-function-in-a-stan-code/40782/3 "2020-06-05T04:11:39Z")

</div>

Many thanks @goedman, I tried with your link but it still not working! I wrote on Stan discourse!
