Jump to content

Call a function in a shared library: Difference between revisions

Added SNOBOL4 example.
(Added SNOBOL4 example.)
Line 1,902:
 
ExtLib openimage: 'test.png'.</lang>
 
=={{header|SNOBOL4}}==
{{works with|CSNOBOL4}}
This code loads the <tt>libm</tt> library into the variable <tt>ffi_m</tt> and binds the <tt>hypot()</tt> function to the variable <tt>ffi_m_hypot</tt>. (The variable names are arbitrary.) It then declares a SNOBOL4 function called <tt>hypot()</tt> which takes two <tt>double</tt>s as arguments and returns a <tt>double</tt>, binding this name to the <tt>ffi_m_hypot</tt> object returned earlier. It then outputs four hypotenuse calculations using those values.
<lang snobol4>-INCLUDE 'ffi.sno'
 
ffi_m = FFI_DLOPEN('/usr/lib/x86_64-linux-gnu/libm.so')
ffi_m_hypot = FFI_DLSYM(ffi_m, 'hypot')
DEFINE_FFI('hypot(double,double)double', ffi_m_hypot)
 
OUTPUT = hypot(1,2)
OUTPUT = hypot(2,3)
OUTPUT = hypot(3,4)
OUTPUT = hypot(4,5)
 
END</lang>
 
Execution looks like this:
 
<pre>$ snobol4 ffitest.sno
2.23606797749979
3.60555127546399
5.
6.40312423743285</pre>
 
=={{header|Tcl}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.