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

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. More specifically, the library I’m interested in is 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.

There is an example of this in the official documentation of Cxx.jl: Examples · Cxx.jl

1 Like

You mean Using C++ with shared libraries?

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

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

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?

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


To come back to the example at hand, notice how the C++ code is compiled:

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

and how the library is loaded within the Julia process:

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)
2 Likes