# Interface with C++ program

**URL:** <https://discourse.mc-stan.org/t/interface-with-c-program/30193>\
**Category:** Developers\
**Created:** [January 28, 2023, 3:09pm UTC](https://discourse.mc-stan.org/t/interface-with-c-program/30193 "2023-01-28T15:09:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![borisdeletic](https://avatars.discourse-cdn.com/v4/letter/b/b9bd4f/32.png) [@borisdeletic](https://discourse.mc-stan.org/u/borisdeletic)\
**Post date:** [January 28, 2023, 3:09pm UTC](https://discourse.mc-stan.org/t/interface-with-c-program/30193/1 "2023-01-28T15:09:09Z")

</div>

Hi,

I’m trying to use Stan as a library in my C++ program for its HMC sampler functionality. I haven’t been able to find any C++ documentation, so my current approach is to reverse engineer the API by reading the unit tests and autogenerated code from stanc compiler.

Is there another approach or some doc resources I’ve missed? I thought this would be a common requirement from users, but have not found much discussion on the topic.

I specifically require a C++ interface and can’t use the OCaml compiler as an intermediary.

Appreciate any help.

---

<div class="post-metadata">

**Author:** ![sakrejda](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/sakrejda/32/845_2.png) [@sakrejda](https://discourse.mc-stan.org/u/sakrejda)\
**Post date:** [January 29, 2023, 11:43am UTC](https://discourse.mc-stan.org/t/interface-with-c-program/30193/2 "2023-01-29T11:43:51Z")

</div>

> [@borisdeletic](#):
>
> Is there another approach or some doc resources I’ve missed? I thought this would be a common requirement from users, but have not found much discussion on the topic.

In the `stan-dev/stan` repo you’ll find `src/services` where there are function calls set up to various versions of the HMC algorithm. They’re used in `stan-dev/cmdstan` so you can look there for what it takes to set up the parameters to call the `services` layer. The construction of the arguments is not all that intuitive (to me anyway) to use externally but overall not bad at all!

Edit: it has not bee a common requirement, many users just use an interface from R/Python and spend all their time writing models. I’d still _like_ for Stan to have a friendlier layer for developing interfaces but I sadly don’t have the free dev time to commit to making that happen -\_(:/)\_-

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [January 30, 2023, 2:28pm UTC](https://discourse.mc-stan.org/t/interface-with-c-program/30193/3 "2023-01-30T14:28:53Z")

</div>

In addition to @sakrejda’s great answer, there are some unofficial resources which you might find useful, such as this good series of blog posts by @jtimonen from last year:

> **[Understanding the Stan codebase - Part 1: Finding an entry point | Juho Timonen](https://jtimonen.github.io/posts/post-01/)**
>
> Overview of the different libraries related to Stan and their organization, and finding and entry point to the internal C++ code.

> **[Understanding the Stan codebase - Part 2: Samplers | Juho Timonen](https://jtimonen.github.io/posts/post-02/)**
>
> Studying the hierarchy of different MCMC sampler classes in Stan.
