Hi, I’m trying to make a convenience wrapper for fetching global variables from a C library. The basic wrapper function works, but multiple-dispatch variants don’t work unless I add some specific lines of debugging output.
Here’s the complete code with debug output that makes it work:
#const lib="/path/to/library/file.so"
function getCGlobal(s::Symbol, t)
println("getCGlobal:", s, ", ",t)
display(s)
display(typeof(s))
display(t)
display(typeof(t))
valptr=cglobal((s, lib),t);
return unsafe_load(valptr);
end
getCGlobalInt(s::Symbol)=getCGlobal(s, Cint);
getCGlobalFloat(s::Symbol)=getCGlobal(s, Cfloat);
now for example I can run either variant to read a global integer in the C library (having already called a C library function that assigns something to this variable):
julia> tts=getCGlobal(:timetostop,Cint)
getCGlobal:timetostop, Int32
:timetostop
Symbol
Int32
DataType
0
julia> tts=getCGlobalInt(:timetostop)
getCGlobal:timetostop, Int32
:timetostop
Symbol
Int32
DataType
0
Now if I comment out any of the println or display lines, the main function still works but the type-specific method fails. For example here’s the output when I redefine the function wihtout the println statement:
julia> tts=getCGlobal(:timetostop,Cint)
:timetostop
Symbol
Int32
DataType
0
julia> tts=getCGlobalInt(:timetostop)
:timetostop
Symbol
Int32
DataType
ERROR: TypeError: in cglobal: first argument not a pointer or valid constant expression, expected Ptr, got a value of type Tuple{Symbol, String}
Stacktrace:
[1] getCGlobal
@ ./REPL[38]:6 [inlined]
[2] getCGlobalInt(s::Symbol)
@ Main ./REPL[39]:1
[3] top-level scope
@ REPL[42]:1
I’m completely at a loss as to why this doesn’t work. I’ve tried this on 2 different linux machines, one running 1.9.0 the other running 1.9.3 and it was the same. Can anyone reproduce this behaviour (sorry this example uses an in-house library but it should be the same for anything that exports global variables).
thanks for any advice
Graham