# How to expose a C++ library to julia

**URL:** https://discourse.julialang.org/t/how-to-expose-a-c-library-to-julia/11068
**Category:** General Usage
**Tags:** question
**Created:** [May 22, 2018, 9:43am UTC](https://discourse.julialang.org/t/how-to-expose-a-c-library-to-julia/11068 "2018-05-22T09:43:04Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)
#### Post date: [May 22, 2018, 9:43am UTC](https://discourse.julialang.org/t/how-to-expose-a-c-library-to-julia/11068/1 "2018-05-22T09:43:04Z")

</div>

I would like to use this C++ library from julia: [https://github.com/ORNL/Tasmanian](https://github.com/ORNL/Tasmanian)  
there is a Python interface which I tried to use with `PyCall` but without success. I’m now wondering how hard it would be to wrap this as a julia package. I have seen both `Cxx.jl` and `CxxWrap.jl`, but somewhere someone mentioned that probably the easiest approach would be to add some `EXTERN "C { } ` calls in the library. I don’t know that means. any hints and ideas much appreciated.

The build creates a shared library object. Is it possible to just link to that somehow? I’m thinking of how easy it is to do a `ccall(:funcname,"library_name")`.

---

<div class="post-metadata">

### Author: ![tshort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tshort/32/43_2.png) [@tshort](https://discourse.julialang.org/u/tshort)
#### Post date: [May 22, 2018, 10:41am UTC](https://discourse.julialang.org/t/how-to-expose-a-c-library-to-julia/11068/2 "2018-05-22T10:41:43Z")

</div>

The “extern C” approach may already be done for that library:

[https://github.com/ORNL/TASMANIAN/blob/ebaa87fe64b415b5d974eeafad962d43c998b7a3/InterfaceFortran/tsgC2Fortran.cpp#L48](https://github.com/ORNL/TASMANIAN/blob/ebaa87fe64b415b5d974eeafad962d43c998b7a3/InterfaceFortran/tsgC2Fortran.cpp#L48)

That creates a C api for the C++ code. You should be able to use that with `ccall`.

---

<div class="post-metadata">

### Author: ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)
#### Post date: [May 22, 2018, 11:27am UTC](https://discourse.julialang.org/t/how-to-expose-a-c-library-to-julia/11068/3 "2018-05-22T11:27:21Z")

</div>

oh, great! so you would suggest to just use the methods from the fortran interface? thanks.

---

<div class="post-metadata">

### Author: ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)
#### Post date: [May 22, 2018, 11:37am UTC](https://discourse.julialang.org/t/how-to-expose-a-c-library-to-julia/11068/4 "2018-05-22T11:37:33Z")

</div>

The easiest approach really depends on the library and the use case. If a C interface already exists, directly using `ccall` is probably the simplest approach. Glancing at the documentation, it looks like the Fortran interface is still experimental, but the Python interface (which looks more complete, also at first glance) also uses a set of C functions, so it may be worth looking into copying that approach. First thing to check is if the required functionality is available through one of the C interfaces, though. If you still need to write a lot of C wrapper functions yourself, then using CxxWrap.jl or Cxx.jl is probably faster once you get over the initial learning curve.

---

<div class="post-metadata">

### Author: ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)
#### Post date: [May 22, 2018, 11:40am UTC](https://discourse.julialang.org/t/how-to-expose-a-c-library-to-julia/11068/5 "2018-05-22T11:40:55Z")

</div>

It looks like there are two C interfaces. One for fortran and one for python:

[https://github.com/ORNL/TASMANIAN/blob/ebaa87fe64b415b5d974eeafad962d43c998b7a3/SparseGrids/TasmanianSparseGrid.cpp#L1481](https://github.com/ORNL/TASMANIAN/blob/ebaa87fe64b415b5d974eeafad962d43c998b7a3/SparseGrids/TasmanianSparseGrid.cpp#L1481)

---

<div class="post-metadata">

### Author: ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)
#### Post date: [May 22, 2018, 11:43am UTC](https://discourse.julialang.org/t/how-to-expose-a-c-library-to-julia/11068/6 "2018-05-22T11:43:50Z")

</div>

awesome! thanks so much guys. seems to work!

```julia
julia> s=ccall((:tsgGetVersion, "/Applications/TSG/lib/libtasmaniansparsegrid.dylib"),Cstring,())
Cstring(0x000000012b831cb4)

julia> unsafe_string(s)
"6.0"

```

---

<div class="post-metadata">

### Author: ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)
#### Post date: [May 23, 2018, 2:39pm UTC](https://discourse.julialang.org/t/how-to-expose-a-c-library-to-julia/11068/7 "2018-05-23T14:39:18Z")

</div>

just checking in to say thanks again: That was quite a breeze! I ended up copying the Python approach. I’ve got the basic functionality down I think.

[https://github.com/floswald/Tasmanian.jl](https://github.com/floswald/Tasmanian.jl)

---

<div class="post-metadata">

### Author: ![tshort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tshort/32/43_2.png) [@tshort](https://discourse.julialang.org/u/tshort)
#### Post date: [May 23, 2018, 2:52pm UTC](https://discourse.julialang.org/t/how-to-expose-a-c-library-to-julia/11068/8 "2018-05-23T14:52:31Z")

</div>

Cool. You might also consider generating binaries for the Tasmanian libraries. [BinaryBuilder](https://github.com/JuliaPackaging/BinaryBuilder.jl) is pretty new, but it works great. It looks like this is a CMake build, so it might be pretty easy. With pre-built libraries, it’ll be easier for users to install the package.

---

<div class="post-metadata">

### Author: ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)
#### Post date: [May 23, 2018, 3:08pm UTC](https://discourse.julialang.org/t/how-to-expose-a-c-library-to-julia/11068/9 "2018-05-23T15:08:29Z")

</div>

ha, that was a timely comment! I was just trying to understand how to do this with `BinDeps`. I’ll skip this then 🙂

---

<div class="post-metadata">

### Author: ![tshort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tshort/32/43_2.png) [@tshort](https://discourse.julialang.org/u/tshort)
#### Post date: [May 23, 2018, 3:50pm UTC](https://discourse.julialang.org/t/how-to-expose-a-c-library-to-julia/11068/10 "2018-05-23T15:50:49Z")

</div>

If you run into problems, your best bet is the `bindeps2` channel on Slack. Or, you can ask here.
