# Calling C++ function having std::vectors as input and output parameters from Julia

**URL:** https://discourse.julialang.org/t/calling-c-function-having-std-vectors-as-input-and-output-parameters-from-julia/9224
**Category:** General Usage
**Tags:** cxx
**Created:** [February 21, 2018, 7:02am UTC](https://discourse.julialang.org/t/calling-c-function-having-std-vectors-as-input-and-output-parameters-from-julia/9224 "2018-02-21T07:02:43Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![pdlotko](https://avatars.discourse-cdn.com/v4/letter/p/49beb7/32.png) [@pdlotko](https://discourse.julialang.org/u/pdlotko)
#### Post date: [February 21, 2018, 7:02am UTC](https://discourse.julialang.org/t/calling-c-function-having-std-vectors-as-input-and-output-parameters-from-julia/9224/1 "2018-02-21T07:02:43Z")

</div>

Dear All  
I am new to Julia and and I am trying to access my C++ code from Julia. I have asked this question already at [Stack Overflow](https://stackoverflow.com/questions/48851198/calling-c-function-having-stdvectors-as-input-and-output-parameters-from-jul), but were not able to find an answer there.  
I am trying to call a C++ function from Julia using Cxx. The input and output parameters of the C++ function are std::vectors, see the function compute\_sum in the example below:

> #include   
> #include
> 
> std::vector\< int \> compute\_sum( const std::vector\< std::vector \>& input )  
> {  
> std::vector\< int \> resut( input.size() , 0 );  
> for ( std::size\_t i = 0 ; i != input.size() ; ++i )  
> {  
> for ( std::size\_t j = 0 ; j != input[i].size() ; ++j )  
> {  
> resut[i] += input[i][j];  
> }  
> }  
> return resut;  
> }
> 
> void simple\_function( int i )  
> {  
> std::cout \<\< "The numbers is : " \<\< i \<\< std::endl;  
> }

Assuming this function is stored as code.cpp I am compiling it to a shared object code.so using:

> g++ -shared -fPIC code.cpp -o code.so

and as a result I obtain a file code.so

Having this, I run Julia in the same folder as code.so. My version of Julia is 0.6.2. Then I import Cxx and the code.so file using:

> julia\> using Cxx  
> julia\> const path\_to\_lib = pwd()  
> julia\> addHeaderDir(path\_to\_lib, kind=C\_System)  
> julia\> Libdl.dlopen(path\_to\_lib \* “/code.so”, Libdl.RTLD\_GLOBAL)  
> Ptr{Void} @0x00000000044bda30  
> julia\> cxxinclude(“code.cpp”)

In odder to test if the process is successful I am calling the simple\_function and obtain the correct results:

> julia\> @cxx simple\_function(1234)  
> The numbers is : 1234

Then I want to call compute\_sum function. For that I need somehow to create, or convert Julia vector into C++ std::vector\< std::vector \>. I am trying the following:

> julia\> cxx" std::vector\< std::vector \> a;"  
> true  
> julia\> icxx" a.push\_back( std::vector(1,2) ); "  
> julia\> icxx" a.push\_back( std::vector(1,3) ); "  
> julia\> icxx" a.push\_back( std::vector(1,4) ); "  
> julia\> icxx" a.size(); "  
> 0x0000000000000003

So I assume that the vector is created in a correct way. Then I trying to call the function with it, but I fail:

> julia\> @cxx compute\_sum(a)  
> ERROR: UndefVarError: a not defined  
> julia\> @cxx compute\_sum(“a”)  
> ERROR: Got bad type information while compiling Cxx.CppNNS{Tuple{:compute\_sum}} (got String for argument 1)  
> julia\> icxx " compute\_sum(a);"  
> ERROR: syntax: extra token “”" after end of expression

Could anyone help me please with the following question(s):

1. How to call compute\_sum function from Julia? I am happy to use any technique (not necessary Cxx) that works and is reasonably fast.
2. How to convert the result of compute\_sum to Julia array?

If one cannot do it with std::vector on C++ side, I would appreciate any advice on how to transfer large vectors (for instance in the form int\*\*) from Julia to C++ and back.

Thank you very much!

pawel

---

<div class="post-metadata">

### Author: ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)
#### Post date: [February 22, 2018, 12:41pm UTC](https://discourse.julialang.org/t/calling-c-function-having-std-vectors-as-input-and-output-parameters-from-julia/9224/2 "2018-02-22T12:41:31Z")

</div>

Hi,

The following code should work in Julia, the trick is to use the `icxx` string macro with interpolation:

```julia
using Cxx

cxx"""
#include <iostream>
#include <vector>

std::vector<int> compute_sum(const std::vector<std::vector<int>> &input)
{
  std::vector<int> result(input.size(), 0);
  for (std::size_t i = 0; i != input.size(); ++i)
  {
    for (std::size_t j = 0; j != input[i].size(); ++j)
    {
      result[i] += input[i][j];
    }
  }
  return result;
}
"""

cxx_v = icxx"std::vector<std::vector<int>>{{1,2},{1,2,3}};"
println("Input vectors:")
for v in cxx_v
  println(" ", collect(v))
end

cxx_sum = icxx"compute_sum($cxx_v);"
println("Cxx sums: $(collect(cxx_sum))")

```

I have also posted this answer on Stack Overflow

---

<div class="post-metadata">

### Author: ![vvjn](https://avatars.discourse-cdn.com/v4/letter/v/5f9b8f/32.png) [@vvjn](https://discourse.julialang.org/u/vvjn)
#### Post date: [February 23, 2018, 7:04pm UTC](https://discourse.julialang.org/t/calling-c-function-having-std-vectors-as-input-and-output-parameters-from-julia/9224/3 "2018-02-23T19:04:32Z")

</div>

If you want to convert a julia array of arrays to std::vectors and pass it to C++, and vice versa, you can do the following.

```julia
as = [rand(1:10,5), rand(1:10,6)]
x = convert(cxxt"std::vector< std::vector< int > > >", as)
collect(icxx"compute_sum($x);")

```

---

<div class="post-metadata">

### Author: ![vvjn](https://avatars.discourse-cdn.com/v4/letter/v/5f9b8f/32.png) [@vvjn](https://discourse.julialang.org/u/vvjn)
#### Post date: [February 23, 2018, 7:07pm UTC](https://discourse.julialang.org/t/calling-c-function-having-std-vectors-as-input-and-output-parameters-from-julia/9224/4 "2018-02-23T19:07:42Z")

</div>

`x = cxxt"std::vector< std::vector< int > > >"(as)` also works. You can convert julia arrays to std::vectors for a number of different types. Cxx also implements the iterator interface on std::vectors, which is why you can call collect on it.

---

<div class="post-metadata">

### Author: ![pdlotko](https://avatars.discourse-cdn.com/v4/letter/p/49beb7/32.png) [@pdlotko](https://discourse.julialang.org/u/pdlotko)
#### Post date: [February 23, 2018, 9:52pm UTC](https://discourse.julialang.org/t/calling-c-function-having-std-vectors-as-input-and-output-parameters-from-julia/9224/5 "2018-02-23T21:52:38Z")

</div>

> [@vvjn](#):
>
> collect(icxx"compute\_sum($x);")

Thank you, that solution works, and this is what I need.

---

<div class="post-metadata">

### Author: ![pdlotko](https://avatars.discourse-cdn.com/v4/letter/p/49beb7/32.png) [@pdlotko](https://discourse.julialang.org/u/pdlotko)
#### Post date: [February 23, 2018, 9:53pm UTC](https://discourse.julialang.org/t/calling-c-function-having-std-vectors-as-input-and-output-parameters-from-julia/9224/6 "2018-02-23T21:53:14Z")

</div>

Thank you for suggesting it. It works as a way of converting Julia arrays to std::vectors.

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [May 16, 2019, 2:48pm UTC](https://discourse.julialang.org/t/calling-c-function-having-std-vectors-as-input-and-output-parameters-from-julia/9224/7 "2019-05-16T14:48:25Z")

</div>

Hello, to me (Julia 1.1, Cxx 0.3.1) this method doesn’t work (while the convert one does):

```julia
julia> x = cxxt"std::vector< std::vector< int > >"(as)
ERROR: MethodError: no method matching Cxx.CxxCore.CppValue{Cxx.CxxCore.CxxQualType{Cxx.CxxCore.CppTemplate{Cxx.CxxCore.CppBaseType{Symbol("std::vector")},Tuple{Cxx.CxxCore.CxxQualType{Cxx.CxxCore.CppTemplate{Cxx.CxxCore.CppBaseType{Symbol("std::vector")},Tuple{Int32,Cxx.CxxCore.CxxQualType{Cxx.CxxCore.CppTemplate{Cxx.CxxCore.CppBaseType{Symbol("std::allocator")},Tuple{Int32}},(false, false, false)}}},(false, false, false)},Cxx.CxxCore.CxxQualType{Cxx.CxxCore.CppTemplate{Cxx.CxxCore.CppBaseType{Symbol("std::allocator")},Tuple{Cxx.CxxCore.CxxQualType{Cxx.CxxCore.CppTemplate{Cxx.CxxCore.CppBaseType{Symbol("std::vector")},Tuple{Int32,Cxx.CxxCore.CxxQualType{Cxx.CxxCore.CppTemplate{Cxx.CxxCore.CppBaseType{Symbol("std::allocator")},Tuple{Int32}},(false, false, false)}}},(false, false, false)}}},(false, false, false)}}},(false, false, false)},N} where N(::Array{Array{Int64,1},1})

```

Also (but it seems irrelevant) there is one exceeding “`>`”

---

<div class="post-metadata">

### Author: ![gabrielfreire](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabrielfreire/32/8799_2.png) [@gabrielfreire](https://discourse.julialang.org/u/gabrielfreire)
#### Post date: [June 23, 2019, 11:15pm UTC](https://discourse.julialang.org/t/calling-c-function-having-std-vectors-as-input-and-output-parameters-from-julia/9224/8 "2019-06-23T23:15:50Z")

</div>

Hi @barche, this code throws this error for me on Windows 10

```julia
LLVM ERROR: Program used external function '__gxx_personality_v0' which could not be resolved!

```

Does anyone know what this means?

this is my Julia

```julia
julia> versioninfo()
Julia Version 1.3.0-DEV.294
Commit 137b21e77c (2019-05-28 15:17 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)

```

---

<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 24, 2019, 9:37am UTC](https://discourse.julialang.org/t/calling-c-function-having-std-vectors-as-input-and-output-parameters-from-julia/9224/9 "2019-06-24T09:37:04Z")

</div>

AFAIK windows support is still WIP, see [this issue](https://github.com/JuliaInterop/Cxx.jl/issues/409) and [PR](https://github.com/JuliaInterop/Cxx.jl/pull/425).

---

<div class="post-metadata">

### Author: ![Hannah\_Rocio\_Santa\_C](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hannah_rocio_santa_c/32/21913_2.png) [@Hannah\_Rocio\_Santa\_C](https://discourse.julialang.org/u/Hannah_Rocio_Santa_C)
#### Post date: [August 30, 2021, 4:21pm UTC](https://discourse.julialang.org/t/calling-c-function-having-std-vectors-as-input-and-output-parameters-from-julia/9224/10 "2021-08-30T16:21:32Z")

</div>

This didn’t work for me, do you know why that might be?

I wrote:

jarr =[2,3]  
carr=convert(cxxt"std::vector\< std::vector\< int \> \> \>", jarr)

and got:

MethodError: Cannot `convert` an object of type Int64 to an object of type Cxx.CxxCore.CppValue{Cxx.CxxCore.CxxQualType{Cxx.CxxCore.CppTemplate{Cxx.CxxCore.CppBaseType{Symbol(“std::\_\_1::vector”)},Tuple{Int32,Cxx.CxxCore.CxxQualType{Cxx.CxxCore.CppTemplate{Cxx.CxxCore.CppBaseType{Symbol(“std::\_\_1::allocator”)},Tuple{Int32}},(false, false, false)}}},(false, false, false)},N} where N  
Closest candidates are:  
convert(::Type{Cxx.CxxCore.CppValue{Cxx.CxxCore.CxxQualType{Cxx.CxxCore.CppTemplate{Cxx.CxxCore.CppBaseType{Symbol(“std::\_\_1::vector”)},Tuple{T,Cxx.CxxCore.CxxQualType{Cxx.CxxCore.CppTemplate{Cxx.CxxCore.CppBaseType{Symbol(“std::\_\_1::allocator”)},Tuple{T}},(false, false, false)}}},(false, false, false)},N} where N}, !Matched::AbstractArray) where T at /Users/hannah/.julia/packages/Cxx/1RaOv/src/std.jl:127  
convert(::Type{T}, !Matched::T) where T at essentials.jl:168  
setindex! at std.jl:143 [inlined]  
copyto!(::IndexLinear, ::Cxx.CxxStd.WrappedCppObjArray{Cxx.CxxCore.CxxQualType{Cxx.CxxCore.CppTemplate{Cxx.CxxCore.CppBaseType{Symbol(“std::\_\_1::vector”)},Tuple{Int32,Cxx.CxxCore.CxxQualType{Cxx.CxxCore.CppTemplate{Cxx.CxxCore.CppBaseType{Symbol(“std::\_\_1::allocator”)},Tuple{Int32}},(false, false, false)}}},(false, false, false)},(false, false, false)}, ::IndexLinear, ::Array{Int64,1}) at abstractarray.jl:807  
copyto! at abstractarray.jl:799 [inlined]  
copy! at std.jl:205 [inlined]  
copy! at std.jl:114 [inlined]  
convert(::Type{Cxx.CxxCore.CppValue{Cxx.CxxCore.CxxQualType{Cxx.CxxCore.CppTemplate{Cxx.CxxCore.CppBaseType{Symbol(“std::\_\_1::vector”)},Tuple{Cxx.CxxCore.CxxQualType{Cxx.CxxCore.CppTemplate{Cxx.CxxCore.CppBaseType{Symbol(“std::\_\_1::vector”)},Tuple{Int32,Cxx.CxxCore.CxxQualType{Cxx.CxxCore.CppTemplate{Cxx.CxxCore.CppBaseType{Symbol(“std::\_\_1::allocator”)},Tuple{Int32}},(false, false, false)}}},(false, false, false)},Cxx.CxxCore.CxxQualType{Cxx.CxxCore.CppTemplate{Cxx.CxxCore.CppBaseType{Symbol(“std::\_\_1::allocator”)},Tuple{Cxx.CxxCore.CxxQualType{Cxx.CxxCore.CppTemplate{Cxx.CxxCore.CppBaseType{Symbol(“std::\_\_1::vector”)},Tuple{Int32,Cxx.CxxCore.CxxQualType{Cxx.CxxCore.CppTemplate{Cxx.CxxCore.CppBaseType{Symbol(“std::\_\_1::allocator”)},Tuple{Int32}},(false, false, false)}}},(false, false, false)}}},(false, false, false)}}},(false, false, false)},N} where N}, ::Array{Int64,1}) at std.jl:129  
top-level scope at C++ try.jl:753

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [August 30, 2021, 4:41pm UTC](https://discourse.julialang.org/t/calling-c-function-having-std-vectors-as-input-and-output-parameters-from-julia/9224/11 "2021-08-30T16:41:21Z")

</div>

Doesn’t that fail simply because you’re trying to convert a vector to a vector of vectors? Try `jarr = [[2,3], [4,5]]` instead (or convert to `std::vector< int >`).

---

<div class="post-metadata">

### Author: ![Hannah\_Rocio\_Santa\_C](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hannah_rocio_santa_c/32/21913_2.png) [@Hannah\_Rocio\_Santa\_C](https://discourse.julialang.org/u/Hannah_Rocio_Santa_C)
#### Post date: [August 30, 2021, 5:12pm UTC](https://discourse.julialang.org/t/calling-c-function-having-std-vectors-as-input-and-output-parameters-from-julia/9224/12 "2021-08-30T17:12:47Z")

</div>

oh! yes! thank you, my bad.  
I fixed that, but it now says “Could not parse type name”?

I tried both converting a vector of vectors(copying the code from here), and a vector (following what you said):

jarr =[2,3]  
carr=convert(cxxt"std::vector\< int \>\>", jarr)

Thanks!

---

<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: [September 9, 2021, 12:20am UTC](https://discourse.julialang.org/t/calling-c-function-having-std-vectors-as-input-and-output-parameters-from-julia/9224/13 "2021-09-09T00:20:08Z")

</div>

> [@Hannah\_Rocio\_Santa\_C](#):
>
> std::vector\< int \>\>

I believe this should be `std::vector< int >`.
