Does Cxx.jl only work with dynamic (shared) libraries, and not static libraries?

I seek to use Cxx.jl to call functions in a C++ library which we usually distribute as a static library, but when I do Libdl.dlopen(lib_path, Libdl.RTLD_GLOBAL), Cxx prints out the error:

ERROR: LoadError: could not load library "/path/to/lib.a"
dlopen(/path/to/lib.a, 9): no suitable image found.  Did find:
    /path/to/lib.a: unknown file type, first eight bytes: 0x21 0x3C 0x61 0x72 0x63 0x68 0x3E 0x0A

However, when I build the library as a dynamic (shared) library, Cxx is happy.

I’m a huge C++ noob so maybe this is a super-stupid question but does Cxx not work with static libraries? This is Julia 0.5.2 and macOS (so Xcode builds the library).

This is not strictly a Cxx.jl issue, dlopen only works on dynamic libraries. Is there a reason it must be a static library? It should be possible to statically link all your library dependencies into one dynamic library, which you can then load with dlopen.

1 Like

Thank you for your explanation! I was able to load the library and run a function through Cxx after compiling the library as dynamic and after ensuring all symbols were exported.

The library uses Intel MKL, etc., so we usually distribute it as a static library. I’m trying to figure out the magical incantation to convert a static library to a dynamic library retaining all the symbols but MKL apparently has several duplicate symbols and I can’t beat macOS’ ld into somehow overcoming that. For testing Cxx though, building it as dynamic is fine.

Unrelated off-topic question—what’s the recommended way to convert a std::complex<double> or std::string to Julia types?

For std::string there is an example in the std.jl test of Cxx.jl. I don’t know if this is implemented for std::complex.

Also, in case you haven’t looked at it already, there is also my CxxWrap.jl package, which uses a different approach by letting you write a wrapper as a C++ dynamic library that is then loaded into Julia. It supports std::string and std::complex conversion, so it might be worth checking out to see what works best for your application.

Absolutely, thanks for answering my questions about a “competitor” to your library! We are evaluating CxxWrap closely and I expect to have some questions for you soon :bowing_man:.