CxxCall.jl icon indicating copy to clipboard operation
CxxCall.jl copied to clipboard

Error when running example

Open gitboy16 opened this issue 3 years ago • 6 comments

Hi, i tried running your example on the readme page and got the following error:

ERROR: UndefVarError: mylib not defined

I was wondering if you could provide a complete example on how to sort a Julia string vector using c++ std::sort?

Thank you

gitboy16 avatar Jan 17 '22 15:01 gitboy16

Hi @gitboy16 you are right, docs are currently lacking. Meantime I recommend looking at the tests. I would start with hello world, does that work locally for you? For the std::sort I can help with that, but it is already a bit advanced. The issue is that julia strings != C++ strings and there are multiple ways to deal with this. I suggest you try to sort a list of Float64 first and then we can look into the string example?

jw3126 avatar Jan 17 '22 15:01 jw3126

Hi, thanks for the feedback. I manage to run the hello example in your test folder but I had to change it a little bit.

I had the following error:

run(`g++ -shared -fPIC $(filepath) -o $libpath`)
clang-13: error: unsupported option '-fPIC' for target 'x86_64-w64-windows-gnu'

I removed the -fPIC and everything worked well. I will try to sort float64 and let you know. Thank you.

gitboy16 avatar Jan 17 '22 16:01 gitboy16

I think I am going to need your help to get std::sort to work on a vector. 😅

gitboy16 avatar Jan 17 '22 18:01 gitboy16

Sure I would try something like this:

@cxx lib function unsafe_sort!(first::Ptr{Float64}, last::Ptr{Float64})::Cvoid
    ...
end

function sort!(v::Vector{Float64}
    GC.@preserve v begin
        unsafe_sort!(...)
    end
end

jw3126 avatar Jan 17 '22 19:01 jw3126

Sorry I did not manage to make it work. I think I am very far from understanding how things work and from your level of knowledge.

gitboy16 avatar Jan 17 '22 20:01 gitboy16

Sorry I did not manage to make it work. I think I am very far from understanding how things work and from your level of knowledge.

Sure generally speaking if a problem is too hard for me to solve directly, I try to solve a simpler subproblem first. Often that subproblem is still too hard, so I slice it down again. This may require a few iterations, until things are simple enough. So here I would recommend calling C code instead of C++ first. Don't use any packages, just ccall. For instance, you could write a simple C function that fills an array with zeros and try to call it from julia.

jw3126 avatar Jan 18 '22 07:01 jw3126