C & C++

Introduction

C and C++ are cornerstone languages in software. While there is conciderable difference between the purposes of Julia and C/C++, they are both important languages in high-performance computing. Importantly, a lot of high-performance software is written in C/C++, so instead of reinventing the wheel it often makes more sense to call such well optimized and tested libraries from other programming languages such as Julia. Furthermore, C/C++ is ubiquitous. It is therefore a good idea to be able to call C/C++ from Julia. As Julia matures, with best-in-class packages such as DifferentialEquations.jl, it is also of increasing importance to be able to call Julia from C/C++.

Julia base has conciderable support for C, as detailed in the manual page "Calling C and Fortran Code". The C interop functionality in Julia base is likely to be the most stable, and it is reasonably well documented. But if you find the API lacking, or want to call C++, you will have to use one of the packages listed below.

Packages

Clang.jl

GitHub Repo stars deps Clang Downloads
Stable Dev GitHub last commit (branch) version Coverage

CBinding.jl

GitHub Repo stars deps CBinding Downloads
Stable Dev GitHub last commit (branch) version Coverage

Cxx.jl

GitHub Repo stars deps Cxx Downloads
Stable Dev GitHub last commit (branch) version Coverage

The Julia C++ Foreign Function Interface (FFI) and REPL.

Despite Cxx.jl being part of the JuliaInterop organization, and both an established and much starred package, the README contains the following warning: "Please, note that Cxx.jl only works (out of the box) currently with Julia 1.1.x to 1.3.x, i.e. with no currently supported Julia, while those versions can still be downloaded at Julialang.org."

You read that right - what may appear like the main package for C++ interop does not work with Julia version > 1.3. It therefore appears that using Cxx.jl is not the best path for most users. I, the current author of this page, do unfortunately now have experience with the alternative packages below, and I am therefore unable to make a generic reccomendation. If you know better, please sumbit a PR and share your knowledge!

CxxWrap.jl

GitHub Repo stars deps CxxWrap Downloads
GitHub last commit (branch) version Coverage

"What's the difference with Cxx.jl?" from its README:

With Cxx.jl it is possible to directly access C++ using the @cxx macro from Julia. So when facing the task of wrapping a C++ library in a Julia package, authors now have two options:

Use Cxx.jl to write the wrapper package in Julia code (much like one uses ccall for wrapping a C library) Use CxxWrap to write the wrapper completely in C++ (and one line of Julia code to load the .so) Boost.Python also uses the latter (C++-only) approach, so translating existing Python bindings based on Boost.Python may be easier using CxxWrap.

jluna

There is also jluna, a younger project. From its README:

Julia is a beautiful language, it is well-designed, and well-documented. Julia's C-API is also well-designed, less beautiful, and much less... documented. jluna aims to fully wrap the official Julia C-API, replacing it in projects with C++ as the host language, by making accessing Julia's unique strengths through C++ safe, hassle-free, and just as beautiful.

References

Star History

This website is a community effort covering a lot of ever-changing information. It will therefore never be complete or without error. If you see something wrong, or have something to contribute, please see the "Contributing" section in the github repository.

Last modified: May 03, 2024. Built with Franklin.jl