# Using Cxx to interface to a C++ library

**URL:** <https://discourse.julialang.org/t/using-cxx-to-interface-to-a-c-library/24969>\
**Category:** General Usage\
**Tags:** cxx\
**Created:** [June 5, 2019, 3:33pm UTC](https://discourse.julialang.org/t/using-cxx-to-interface-to-a-c-library/24969 "2019-06-05T15:33:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hros](https://avatars.discourse-cdn.com/v4/letter/h/97f17d/32.png) [@hros](https://discourse.julialang.org/u/hros)\
**Post date:** [June 5, 2019, 3:33pm UTC](https://discourse.julialang.org/t/using-cxx-to-interface-to-a-c-library/24969/1 "2019-06-05T15:33:15Z")

</div>

I am trying to interface a c++ library ([ntl](https://shoup.net/ntl)) and am thinking of using the [Cxx](https://github.com/JuliaInterop/Cxx.jl) module.

Specifically, I’m looking to use the included C++ types directly in Julia, and perform mathematical operations on them using the C++ overloaded mathematical operators.  
Is there a similar project which can be used a reference?

If not, can someone provide an outline of the necessary code?

Lastly, is this approach (using Cxx) the best approach in terms of runtime performance?

---

<div class="post-metadata">

**Author:** ![oatlzzvztd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oatlzzvztd/32/3285_2.png) [@oatlzzvztd](https://discourse.julialang.org/u/oatlzzvztd)\
**Post date:** [June 5, 2019, 6:43pm UTC](https://discourse.julialang.org/t/using-cxx-to-interface-to-a-c-library/24969/2 "2019-06-05T18:43:58Z")

</div>

A quick github search for “using Cxx” brought up three or four repositories but none that looked to me like Julia v1.x.

---

<div class="post-metadata">

**Author:** ![cserteGT3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csertegt3/32/8283_2.png) [@cserteGT3](https://discourse.julialang.org/u/cserteGT3)\
**Post date:** [June 5, 2019, 7:54pm UTC](https://discourse.julialang.org/t/using-cxx-to-interface-to-a-c-library/24969/3 "2019-06-05T19:54:59Z")

</div>

> [@oatlzzvztd](#):
>
> A quick github search for “using Cxx” brought up three or four repositories but none that looked to me like Julia v1.x.

As far as I know [`Cxx.jl`](https://github.com/JuliaInterop/Cxx.jl) works with v1.1.0 (except windows, which has some issues).

---

<div class="post-metadata">

**Author:** ![oatlzzvztd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oatlzzvztd/32/3285_2.png) [@oatlzzvztd](https://discourse.julialang.org/u/oatlzzvztd)\
**Post date:** [June 5, 2019, 8:35pm UTC](https://discourse.julialang.org/t/using-cxx-to-interface-to-a-c-library/24969/4 "2019-06-05T20:35:34Z")

</div>

> [@cserteGT3](#):
>
> As far as I know [`Cxx.jl`](https://github.com/JuliaInterop/Cxx.jl) works with v1.1.0 (except windows, which has some issues).

Ah, sorry. I didn’t intend to imply that Cxx.jl doesn’t work on v1.1 – it does (and I have used it). I only meant that the project repos I found on github were using an older version of julia and Cxx.jl, which may not be helpful to OP.

---

<div class="post-metadata">

**Author:** ![hros](https://avatars.discourse-cdn.com/v4/letter/h/97f17d/32.png) [@hros](https://discourse.julialang.org/u/hros)\
**Post date:** [June 5, 2019, 8:48pm UTC](https://discourse.julialang.org/t/using-cxx-to-interface-to-a-c-library/24969/5 "2019-06-05T20:48:56Z")

</div>

Since you’ve used Cxx, based on your experience how would you implement a wrapper for a mathematical types.  
The types are declared in C++ as a class with overloaded versions of all basic math operations as well as additional methods.  
I would like to import these types and functionality to be used from Julia, declaring new variables of these types in julia, converting to/from other types etc…

I was able to perform cxximport of the include file and ldlib open of the .so file, and I would appreciate help in the actual implementation (its easy to perform the c++ code with cxx"“”…“”" blocks, but that is not a true integration with julia)

---

<div class="post-metadata">

**Author:** ![cserteGT3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csertegt3/32/8283_2.png) [@cserteGT3](https://discourse.julialang.org/u/cserteGT3)\
**Post date:** [June 5, 2019, 9:40pm UTC](https://discourse.julialang.org/t/using-cxx-to-interface-to-a-c-library/24969/6 "2019-06-05T21:40:37Z")

</div>

> [@oatlzzvztd](#):
>
> Ah, sorry. I didn’t intend to imply that Cxx.jl doesn’t work on v1.1 – it does (and I have used it). I only meant that the project repos I found on github were using an older version of julia and Cxx.jl, which may not be helpful to OP.

My bad, sorry for misunderstooding.

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [June 6, 2019, 7:42am UTC](https://discourse.julialang.org/t/using-cxx-to-interface-to-a-c-library/24969/7 "2019-06-06T07:42:15Z")

</div>

this live demo from keno about wrapping quaternion might help:

[![](https://global.discourse-cdn.com/julialang/original/3X/1/f/1f3be3943ab3f5bd314b41bcf77e8b92af1a26d8.jpeg "C++Now 2018: Keno Fischer & Bart Janssens “The Julia Language and C++: The Perfect Marriage?”") ](https://www.youtube.com/watch?v=hvnxY3NjHQ4&t=3183)

---

<div class="post-metadata">

**Author:** ![hros](https://avatars.discourse-cdn.com/v4/letter/h/97f17d/32.png) [@hros](https://discourse.julialang.org/u/hros)\
**Post date:** [June 25, 2019, 4:18pm UTC](https://discourse.julialang.org/t/using-cxx-to-interface-to-a-c-library/24969/8 "2019-06-25T16:18:30Z")

</div>

I’ve seen this demo  
I have struggled a bit with Cxx, It was easy enough to connect to the shared library (Libdl.dlopen) and open the library header with cxxinclude. However, the documentation is not for sufficient for me to wrap the actual C++ types. For example, I would like to define a Julia type ZZ which will server as a reference / alias to the C++ class ZZ. Then I would like the most succinct and efficient way to enable Julia native math operation on this type to call the corresponding C++ operators and interact with primitive Julia types using the C++ conversion functions.

1. Can anyone share sample code that demonstrates how this could be done?

2. Has anyone measured the performance penalty incurred by this cross-language wrappers?

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [June 26, 2019, 12:28pm UTC](https://discourse.julialang.org/t/using-cxx-to-interface-to-a-c-library/24969/9 "2019-06-26T12:28:33Z")

</div>

you could take a look at std.jl for some examples on how to wrap C++ code:

[https://github.com/JuliaInterop/Cxx.jl/blob/8c2b5c14519e82eb933fadf3f3df66b7bf1b41aa/src/std.jl#L13](https://github.com/JuliaInterop/Cxx.jl/blob/8c2b5c14519e82eb933fadf3f3df66b7bf1b41aa/src/std.jl#L13)

---

<div class="post-metadata">

**Author:** ![hros](https://avatars.discourse-cdn.com/v4/letter/h/97f17d/32.png) [@hros](https://discourse.julialang.org/u/hros)\
**Post date:** [June 30, 2019, 6:25pm UTC](https://discourse.julialang.org/t/using-cxx-to-interface-to-a-c-library/24969/10 "2019-06-30T18:25:54Z")

</div>

Thanks for the pointer  
I have an initial working version (see below)  
Before I continue wrapping the rest of the library, please review and comment if this is the “correct” way, both in terms of ease of use and more importantly in terms of performance (i.e. making sure there are no superfluous object constructions and copying)

```julia
using Cxx
using Libdl

Libdl.dlopen("/usr/local/lib/libntl.so", Libdl.RTLD_GLOBAL)

addHeaderDir("/usr/local/include", kind=C_System)

cxxinclude("NTL/ZZ.h")
cxxinclude("sstream")
cxxinclude("string")

const NTL_ZZ = cxxt"NTL::ZZ"
cxx"""
std::string zzToString(const NTL::ZZ& z) {
    std::stringstream buffer;
    buffer << z;
    return buffer.str();
}
"""
struct ZZ <: Number
    val::NTL_ZZ
    ZZ(n) = new(@cxx NTL::ZZ(n))
end
Base.:+(x::ZZ, y::ZZ) = ZZ(@cxx(x.val+y.val))
Base.:-(x::ZZ, y::ZZ) = ZZ(@cxx(x.val-y.val))
Base.:*(x::ZZ, y::ZZ) = ZZ(@cxx(x.val*y.val))
Base.:/(x::ZZ, y::ZZ) = ZZ(@cxx(x.val/y.val))
Base.:convert(::Type{ZZ}, x::T) where {T<:Integer} = ZZ(x)
Base.:promote_rule(::Type{ZZ}, ::Type{I}) where {I<:Integer} = ZZ
function Base.show(io::IO, x::ZZ)
    zz_str = @cxx zzToString(x.val)
    print(io, unsafe_string(zz_str))
end

```
