# Call C++ library from Julia with the Cxx package?

**URL:** https://discourse.julialang.org/t/call-c-library-from-julia-with-the-cxx-package/30661
**Category:** General Usage
**Tags:** cxx
**Created:** [November 3, 2019, 5:39pm UTC](https://discourse.julialang.org/t/call-c-library-from-julia-with-the-cxx-package/30661 "2019-11-03T17:39:01Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)
#### Post date: [November 3, 2019, 5:39pm UTC](https://discourse.julialang.org/t/call-c-library-from-julia-with-the-cxx-package/30661/1 "2019-11-03T17:39:01Z")

</div>

Hi, I was wondering, does anyone have similar experiences as calling C++ library from Julia? I have only found one reference [Calling C++ library (CGAL) from Julia](https://discourse.julialang.org/t/calling-c-library-cgal-from-julia/21629). More specifically, the library I’m interested in is [https://github.com/osmcode/libosmium](https://github.com/osmcode/libosmium)  
Is it possible that I can call this library using the Cxx package in Julia? If so, how to proceed? I have played with Cxx package for a bit and I think using shared library might be a promising route to follow. Any references and comments are greatly appreciated indeed.

---

<div class="post-metadata">

### Author: ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)
#### Post date: [November 3, 2019, 7:49pm UTC](https://discourse.julialang.org/t/call-c-library-from-julia-with-the-cxx-package/30661/2 "2019-11-03T19:49:10Z")

</div>

There is an example of this in the official documentation of Cxx.jl: [Examples · Cxx.jl](https://juliainterop.github.io/Cxx.jl/dev/examples/#Using-C-with-shared-libraries-1)

---

<div class="post-metadata">

### Author: ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)
#### Post date: [November 4, 2019, 8:12am UTC](https://discourse.julialang.org/t/call-c-library-from-julia-with-the-cxx-package/30661/3 "2019-11-04T08:12:14Z")

</div>

You mean [Using C++ with shared libraries](https://juliainterop.github.io/Cxx.jl/dev/examples/#Using-C-with-shared-libraries-1)?

---

<div class="post-metadata">

### Author: ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)
#### Post date: [November 4, 2019, 8:19am UTC](https://discourse.julialang.org/t/call-c-library-from-julia-with-the-cxx-package/30661/4 "2019-11-04T08:19:36Z")

</div>

Yes (I actually don’t see any difference between both links 😉 )

Isn’t that representative of what you were looking for?

---

<div class="post-metadata">

### Author: ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)
#### Post date: [November 4, 2019, 8:44am UTC](https://discourse.julialang.org/t/call-c-library-from-julia-with-the-cxx-package/30661/5 "2019-11-04T08:44:11Z")

</div>

Hi, thank you for your quick reply. I think it should work out. So why is it called shared library though, is there any difference between a standard c++ library and shared library?

---

<div class="post-metadata">

### Author: ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)
#### Post date: [November 4, 2019, 8:59am UTC](https://discourse.julialang.org/t/call-c-library-from-julia-with-the-cxx-package/30661/6 "2019-11-04T08:59:55Z")

</div>

Well, this has nothing to do with Julia; mostly stuff coming initially from the compiled languages world and the way libraries are distributed and reused by others in an OS.

In short, when you compile C/C++/Fortran/whatever code into a library, you basically have two choices: either use a static library (usually a file with a “.a” extension on Linux & friends) or a shared one (sometimes also referred to as dynamic, usually a file with a “.so” extension on Linux & friends). These two keywords (static vs shared) should give you access to plenty of resources on the web; see for example this SO thread:

[https://stackoverflow.com/questions/2649334/difference-between-static-and-shared-libraries](https://stackoverflow.com/questions/2649334/difference-between-static-and-shared-libraries)

  

To come back to the [example](https://juliainterop.github.io/Cxx.jl/dev/examples/#Using-C-with-shared-libraries-1) at hand, notice how the C++ code is compiled:

```nohighlight
g++ -shared -fPIC ArrayMaker.cpp -o libarraymaker.so

```

and how the library is loaded within the Julia process:

```julia
julia> Libdl.dlopen(joinpath(path_to_lib, "libarraymaker.so"), Libdl.RTLD_GLOBAL)
Ptr{Nothing} @0x00007f9dd4556d60

```

These are full with specificities of shared / dynamic libraries:

- the `-shared` switch to `g++`
- the `.so` extension
- the use of `Libdl` (“DL” stands for Dynamic Linking)
