Skip to content

JuliaInterop/libcxxwrap-julia

Repository files navigation

JlCxx

test-linux-mac test-win

This is the C++ library component of the CxxWrap.jl package, distributed as a regular CMake library for use in other C++ projects. To build a Julia interface to a C++ library, you need to build against this library and supply the resulting library as a binary dependency to your Julia package. The testlib-builder directory contains a complete example of how to build and distribute these binaries, or you can use the BinaryBuilder.jl wizard to generate the builder repository.

See the CxxWrap.jl README for more info on the API.

Building libcxxwrap-julia

Obtaining the source

Simply clone this repository, e.g.:

git clone https://github.com/JuliaInterop/libcxxwrap-julia.git

You can also manage everything directly from Visual Studio Code using "clone git repository" on the start screen.

Preparing the install location

Julia loads the libcxxwrap-julia through the libcxxwrap_julia_jll package, which is built automatically by BinaryBuilder. To tell Julia to use self-compiled binaries, one way is to use an override directory. To do this, check out libcxxwrap_julia_jll for development:

pkg> develop libcxxwrap_julia_jll

Next, import the package and call the dev_jll function:

julia> import libcxxwrap_julia_jll
julia> libcxxwrap_julia_jll.dev_jll()

Configuring and building

The last command should output the path to the devved JLL, e.g. /home/user/.julia/dev/libcxxwrap_julia_jll. In that directory, there will be a subdirectory called override which will contain the binaries from the Julia artifacts distribution. This directory should be completely emptied and then used as build directory from cmake, e.g. in a terminal do:

cd /home/user/.julia/dev/libcxxwrap_julia_jll/override
rm -rf *
cmake -DJulia_PREFIX=/home/user/path/to/julia /path/to/source/of/libcxxwrap-julia
cmake --build . --config Release

Here, /path/to/source/of/libcxxwrap-julia is the cloned git repository.

Building libcxxwrap-julia requires a C++ compiler which supports C++17 (e.g. GCC 7, clang 5; for macOS users that means Xcode 9.3).

The main CMake option of interest is Julia_PREFIX, which should point to the Julia installation you want to use. The PREFIX is a directory, one containing the bin and lib directories and so on. If you are using a binary download of Julia (https://julialang.org/downloads/), this is the top-level directory; if you build Julia from source (https://github.com/JuliaLang/julia), it would be the usr directory of the repository. Below we will call this directory /home/user/path/to/julia, but you should substitute your actual path in the commands below.

Instead of specifying the prefix, it is also possible to directly set the Julia executable, using:

cmake -DJulia_EXECUTABLE=/home/user/path/to/julia/bin/julia ../libcxxwrap-julia

Next, you can build your own code against this by setting the JlCxx_DIR CMake variable to the build directory (libcxxwrap-julia-build) used above, or add it to the CMAKE_PREFIX_PATH CMake variable.

Using the compiled libcxxwrap-julia in CxxWrap

Following the above instructions, Julia will now automatically use the compiled binaries. You can verify this using:

julia> using CxxWrap
julia> CxxWrap.prefix_path()
"/home/user/.julia/dev/libcxxwrap_julia_jll/override"

An alternative method of using self-compiled binaries is the Overrides.toml file, to be placed in .julia/artifacts:

[3eaa8342-bff7-56a5-9981-c04077f7cee7]
libcxxwrap_julia = "/path/to/libcxxwrap-julia-build"

More details here.

The file can be generated automatically using the OVERRIDES_PATH, OVERRIDE_ROOT and APPEND_OVERRIDES_TOML Cmake options, with the caveat that each CMake run will append again to the file and make it invalid, i.e. this is mostly intended for use on CI (see the appveyor and travis files for examples).

Building on Windows

On Windows, building is easiest with Visual Studio 2019, for which the Community Edition with C++ support is a free download. You can clone the https://github.com/JuliaInterop/libcxxwrap-julia.git repository using the built-in git support, and configure the Julia_PREFIX option from the built-in CMake support. See the Visual Studio docs for more info.

Binaries for the main branch

Binaries for the main branch are published at https://github.com/barche/libcxxwrap_julia_jll.jl, you can install them (in Pkg mode, hit ]) using:

add https://github.com/barche/libcxxwrap_julia_jll.jl.git

Using libcxxwrap-julia as a dependency for downstream packages

When developing Julia bindings for a C++ package using libcxxwrap-julia, you need to build against the correct version of libcxxwrap-julia. Specifically, to make sure that your wrapper works correctly with CxxWrap.jl, check the compat settings for libcxxwrap_jll in the Project.toml file of the latest release of CxxWrap.jl. It may happen that the latest release of libcxxwrap-julia is not yet supported by CxxWrap.jl, in which case you should build against an older release (see also this comment.