# Call c++ library from julia

**URL:** https://discourse.julialang.org/t/call-c-library-from-julia/30653
**Category:** General Usage
**Tags:** cxx
**Created:** [November 3, 2019, 2:39pm UTC](https://discourse.julialang.org/t/call-c-library-from-julia/30653 "2019-11-03T14:39:11Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)
#### Post date: [November 3, 2019, 2:39pm UTC](https://discourse.julialang.org/t/call-c-library-from-julia/30653/1 "2019-11-03T14:39:11Z")

</div>

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:

```julia
(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](https://github.com/JuliaInterop/CxxWrap.jl)

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

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [November 3, 2019, 3:17pm UTC](https://discourse.julialang.org/t/call-c-library-from-julia/30653/2 "2019-11-03T15:17:53Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [November 3, 2019, 3:23pm UTC](https://discourse.julialang.org/t/call-c-library-from-julia/30653/3 "2019-11-03T15:23:49Z")

</div>

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](https://github.com/JunoLab/Juno.jl/issues/265).

---

<div class="post-metadata">

### Author: ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)
#### Post date: [November 3, 2019, 4:15pm UTC](https://discourse.julialang.org/t/call-c-library-from-julia/30653/4 "2019-11-03T16:15:47Z")

</div>

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.

```julia
(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
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

```

---

<div class="post-metadata">

### Author: ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)
#### Post date: [November 3, 2019, 4:17pm UTC](https://discourse.julialang.org/t/call-c-library-from-julia/30653/5 "2019-11-03T16:17:10Z")

</div>

Could you provide some examples?

---

<div class="post-metadata">

### Author: ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)
#### Post date: [November 3, 2019, 4:28pm UTC](https://discourse.julialang.org/t/call-c-library-from-julia/30653/6 "2019-11-03T16:28:39Z")

</div>

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

```julia
(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`

```

---

<div class="post-metadata">

### Author: ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)
#### Post date: [November 3, 2019, 5:16pm UTC](https://discourse.julialang.org/t/call-c-library-from-julia/30653/7 "2019-11-03T17:16:44Z")

</div>

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