Call a function in a shared library: Difference between revisions

→‎{{header|Crystal}}: added second part of the problem, falling back to a different function
(→‎{{header|Crystal}}: add crystal implementation)
(→‎{{header|Crystal}}: added second part of the problem, falling back to a different function)
Line 277:
=={{header|Crystal}}==
 
<lang ruby>libm = LibC.dlopen("libm.so.6", LibC::RTLD_LAZY)
sqrtptr = LibC.dlsym(libm, "sqrt") unless libm.null?
libm.value.not_nil!
 
if sqrtptr
sqrtptr = LibC.dlsym(libm, "sqrt")
sqrtproc = Proc(Float64, Float64).new sqrtptr, Pointer(Void).null
sqrtptr.value.not_nil!
at_exit { LibC.dlclose(libm)</lang> }
else
sqrtproc = ->Math.sqrt(Float64)
end
 
puts "the sqrt of 4 is #{sqrtproc.call(4.0)}"</lang>
sqrtproc = Proc(Float64, Float64).new sqrtptr, Pointer(Void).null
puts "the sqrt of 4 is #{sqrtproc.call(4.0)}"
 
LibC.dlclose(libm)</lang>
 
=={{header|D}}==
Anonymous user