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

cxxinclude fail in the example code

Open yjiangnan opened this issue 5 years ago • 3 comments

I installed the recent Cxx.jl master branch on MacOS by Pkg.checkout("Cxx") and Pkg.build("Cxx").

Following "Example 8: Using C++ with shared libraries" on https://github.com/Keno/Cxx.jl, I get errors:

shell> g++ -shared -fPIC ArrayMaker.cpp -o libArrayMaker.so

julia> const path_to_lib = pwd()
"/Users/yjia/Documents/software/source"

julia> addHeaderDir(path_to_lib, kind=C_System)

julia> Libdl.dlopen(path_to_lib * "/libArrayMaker.so", Libdl.RTLD_GLOBAL)
Ptr{Void} @0x00007fdacf83e6d0

julia> cxxinclude("ArrayMaker.h")
ERROR: Could not include file ArrayMaker.h
Stacktrace:
 [1] #cxxinclude#27(::Bool, ::Function, ::Cxx.ClangCompiler, ::String) at /Applications/JuliaPro-0.6.4.1.app/Contents/Resources/pkgs-0.6.4.1/v0.6/Cxx/src/initialization.jl:84
 [2] (::Cxx.#kw##cxxinclude)(::Array{Any,1}, ::Cxx.#cxxinclude, ::Cxx.ClangCompiler, ::String) at ./<missing>:0 (repeats 2 times)
 [3] cxxinclude(::String) at /Applications/JuliaPro-0.6.4.1.app/Contents/Resources/pkgs-0.6.4.1/v0.6/Cxx/src/initialization.jl:90
 [4] macro expansion at /Applications/JuliaPro-0.6.4.1.app/Contents/Resources/pkgs-0.6.4.1/v0.6/Atom/src/repl.jl:118 [inlined]
 [5] anonymous at ./<missing>:?

"ArrayMaker.h" is in the current directory.

How can this problem be solved?

yjiangnan avatar Aug 26 '18 22:08 yjiangnan

The problem magically disappeared a few days later automatically, maybe because of Mac OSX crashing and restarting. However, another problem occurred when I followed the example:

julia> cxxinclude("ArrayMaker.h")

julia> maker = @cxxnew ArrayMaker(5, 2.0)
Got arguments: 5, and 2
(class ArrayMaker *) @0x00007fee908c9670


julia> arr = @cxx maker->fillArr()
Filling the array
Ptr{Float32} @0x00007fee90865290

julia> pointer_to_array(arr, 5)
ERROR: UndefVarError: pointer_to_array not defined
Stacktrace:
 [1] macro expansion at /Applications/JuliaPro-0.6.4.1.app/Contents/Resources/pkgs-0.6.4.1/v0.6/Atom/src/repl.jl:118 [inlined]
 [2] anonymous at ./<missing>:?

julia> arr
Ptr{Float32} @0x00007fee90865290

julia> pointer_to_array(arr, 5)
ERROR: UndefVarError: pointer_to_array not defined
Stacktrace:
 [1] macro expansion at /Applications/JuliaPro-0.6.4.1.app/Contents/Resources/pkgs-0.6.4.1/v0.6/Atom/src/repl.jl:118 [inlined]
 [2] anonymous at ./<missing>:?

julia> pointer
pointer (generic function with 20 methods)

julia> parentindexes
parentindexes (generic function with 3 methods)

How can a builtin function not be defined?

yjiangnan avatar Sep 11 '18 21:09 yjiangnan

pointer_to_array is an old function; the documentation here is simply out of date. The function was deprecated in Julia 0.5 and removed in Julia 0.6. It should now be unsafe_wrap(Array, arr, 5).

ararslan avatar Sep 11 '18 21:09 ararslan

@ararslan Great! Thanks for the info. It seems that the Julia community is still really tiny, though. Very basic things could fail without clear reasons and documentations are either scarce or out of date.

yjiangnan avatar Sep 11 '18 21:09 yjiangnan