Call c++ library from julia

I need to call a C++ library from Julia. I was thinking to try either Cxx package or CxxWrap package. For Cxx, when I was trying to install this Cxx package to my Julia, get the following error message:

(v1.1) pkg> add Cxx
  Updating registry at `~/.julia/registries/General`
  Updating git-repo `https://github.com/JuliaRegistries/General.git`
 Resolving package versions...
ERROR: Unsatisfiable requirements detected for package Atom [c52e3926]:
 Atom [c52e3926] log:
 ├─possible versions are: [0.8.0-0.8.8, 0.9.0-0.9.1, 0.10.0-0.10.2, 0.11.0-0.11.3] or uninstalled
 └─restricted to versions 0.7.14 by an explicit requirement — no versions left

For CxxWrap package, it is not easy for me to understand the boost python hello world example from the github link: GitHub - JuliaInterop/CxxWrap.jl: Package to make C++ libraries available in Julia

Could anyone help to provide any references ? Any comments are greatly appreciated.

If you only need a few functions, I’ve found declaring the functions you wish to call extern "C" (creating wrappers if necessary), compiling into a shared library, and then ccall-ing them to be the simplest approach.

5 Likes

To install Cxx, add it to a separate project that doesn’t contain the Juno packages. See also https://github.com/JunoLab/Juno.jl/issues/265.

4 Likes

Thank you for your quick reply. I’m new to Cxx package. I tried to install Cxx in a clean environment, and it did not work either, sigh.

(v1.0) pkg> activate ~/Desktop/Cxx
[ Info: activating new environment at ~/~/Desktop/Cxx.

(Cxx) pkg> activate

(v1.0) pkg> activate /Desktop/Cxx
[ Info: activating new environment at /Desktop/Cxx.

(Cxx) pkg> add Cxx
 Resolving package versions...
ERROR: Unsatisfiable requirements detected for package Cxx [a0b5b9ef]:
 Cxx [a0b5b9ef] log:
 ├─possible versions are: 0.3.0-0.3.2 or uninstalled
 ├─restricted to versions * by an explicit requirement, leaving only versions 0.3.0-0.3.2
 └─restricted by julia compatibility requirements to versions: uninstalled — no versions left

I also tried to remove Juno package and install Cxx again, it did not work either.

julia> Pkg.status()
    Status `~/.julia/environments/v1.0/Project.toml`
  [c52e3926] Atom v0.9.1
  [336ed68f] CSV v0.5.11
  [324d7699] CategoricalArrays v0.5.5
  [34da2185] Compat v2.1.0
  [8f4d0f93] Conda v1.3.0
  [1f15a43c] CxxWrap v0.8.2
  [a93c6f00] DataFrames v0.19.3
  [864edb3b] DataStructures v0.17.0
  [31a5f54b] Debugger v0.6.2
  [31c24e10] Distributions v0.21.1
  [cd3eb016] HTTP v0.8.5
  [682c06a0] JSON v0.21.0
  [fc18253b] LazyJSON v0.1.1
  [522f3ed2] LibExpat v0.5.0
  [093fc24a] LightGraphs v1.3.0
  [9c8b4983] LightXML v0.8.0
  [86cd37e6] OpenStreetMapX v0.1.11 [`~/.julia/dev/OpenStreetMapX`]
  [d3d4fdd0] OpenStreetMapXPlot v0.1.2 #master (https://github.com/pszufe/OpenStreetMapXPlot.jl)
  [91a5bcdd] Plots v0.26.2
  [438e738f] PyCall v1.91.2
  [ee283ea6] Rebugger v0.3.2
  [189a3867] Reexport v0.2.0
  [295af30f] Revise v2.2.0
  [90137ffa] StaticArrays v0.11.0
  [f3b207a7] StatsPlots v0.10.2
  [9a3f8284] Random 

(v1.0) pkg> add Cxx
 Resolving package versions...
ERROR: Unsatisfiable requirements detected for package Cxx [a0b5b9ef]:
 Cxx [a0b5b9ef] log:
 ├─possible versions are: 0.3.0-0.3.2 or uninstalled
 ├─restricted to versions * by an explicit requirement, leaving only versions 0.3.0-0.3.2
 └─restricted by julia compatibility requirements to versions: uninstalled — no versions left

Could you provide some examples?

I tried julia version 1.2 and it worked. It’s a typical example of “my computer works and I do not know why” :wink:

(v1.2) pkg> add Cxx
  Updating registry at `~/.julia/registries/General`
  Updating git-repo `https://github.com/JuliaRegistries/General.git`
 Resolving package versions...
 Installed JuliaFormatter ─ v0.1.19
 Installed Cxx ──────────── v0.3.2
  Updating `~/.julia/environments/v1.2/Project.toml`
  [a0b5b9ef] + Cxx v0.3.2
  Updating `~/.julia/environments/v1.2/Manifest.toml`
  [b99e7846] ↑ BinaryProvider v0.5.6 ⇒ v0.5.7
  [a0b5b9ef] + Cxx v0.3.2
  [1a297f60] ↑ FillArrays v0.7.3 ⇒ v0.7.4
  [f6369f11] ↑ ForwardDiff v0.10.3 ⇒ v0.10.5
  [28b8d3ca] ↑ GR v0.41.0 ⇒ v0.42.0
  [eafb193a] ↑ Highlights v0.4.4 ⇒ v0.4.5
  [a98d9a8b] ↑ Interpolations v0.12.2 ⇒ v0.12.4
  [98e50ef6] ↑ JuliaFormatter v0.1.16 ⇒ v0.1.19
  [429524aa] ↑ Optim v0.19.3 ⇒ v0.19.4
  [85a6dd25] ↑ PositiveFactorizations v0.2.2 ⇒ v0.2.3
  Building Cxx → `~/.julia/packages/Cxx/vxYtJ/deps/build.log`

I’ve already got some examples to use shared library and Cxx package.