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

**URL:** <https://discourse.julialang.org/t/does-cxx-jl-only-work-with-dynamic-shared-libraries-and-not-static-libraries/4253>\
**Category:** General Usage\
**Tags:** cxx\
**Created:** [June 13, 2017, 8:08pm UTC](https://discourse.julialang.org/t/does-cxx-jl-only-work-with-dynamic-shared-libraries-and-not-static-libraries/4253 "2017-06-13T20:08:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fasiha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fasiha/32/1146_2.png) [@fasiha](https://discourse.julialang.org/u/fasiha)\
**Post date:** [June 13, 2017, 8:08pm UTC](https://discourse.julialang.org/t/does-cxx-jl-only-work-with-dynamic-shared-libraries-and-not-static-libraries/4253/1 "2017-06-13T20:08:05Z")

</div>

I seek to use [Cxx.jl](https://github.com/Keno/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:

```julia
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).

---

<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:** [June 13, 2017, 8:41pm UTC](https://discourse.julialang.org/t/does-cxx-jl-only-work-with-dynamic-shared-libraries-and-not-static-libraries/4253/2 "2017-06-13T20:41:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fasiha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fasiha/32/1146_2.png) [@fasiha](https://discourse.julialang.org/u/fasiha)\
**Post date:** [June 14, 2017, 2:29am UTC](https://discourse.julialang.org/t/does-cxx-jl-only-work-with-dynamic-shared-libraries-and-not-static-libraries/4253/3 "2017-06-14T02:29:59Z")

</div>

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?

---

<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:** [June 14, 2017, 5:57am UTC](https://discourse.julialang.org/t/does-cxx-jl-only-work-with-dynamic-shared-libraries-and-not-static-libraries/4253/4 "2017-06-14T05:57:24Z")

</div>

> [@fasiha](#):
>
> 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](https://github.com/JuliaInterop/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.

---

<div class="post-metadata">

**Author:** ![fasiha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fasiha/32/1146_2.png) [@fasiha](https://discourse.julialang.org/u/fasiha)\
**Post date:** [June 14, 2017, 5:43pm UTC](https://discourse.julialang.org/t/does-cxx-jl-only-work-with-dynamic-shared-libraries-and-not-static-libraries/4253/5 "2017-06-14T17:43:20Z")

</div>

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 🙇‍♂️.
