Call a function in a shared library: Difference between revisions
Call a function in a shared library (view source)
Revision as of 22:01, 24 September 2019
, 4 years agoAdded Perl example
m (→{{header|Go}}: Corrected typo.) |
SqrtNegInf (talk | contribs) (Added Perl example) |
||
Line 1,364:
=={{header|Pascal}}==
See [[Call_a_function_in_a_shared_library#Delphi | Delphi]]
=={{header|Perl}}==
Examples for simple <code>C</code> library calls, but each module is capable of much more (and can work with other languages). Refer to their documentation for details.
===Inline===
This modules auto-builds a wrapper to the library on the first call, and subsequently uses that interface with no delay.
<lang perl>use Inline
C => "DATA",
ENABLE => "AUTOWRAP",
LIBS => "-lm";
print 4*atan(1) . "\n";
__DATA__
__C__
double atan(double x);</lang>
{{out}}
<pre>3.14159265358979</pre>
===FFI===
This module is smart about finding libraries, here getting <code>atan</code> (from 'lm') and <code>puts</code> (from 'libc').
<lang perl>use FFI::Platypus;
my $ffi = FFI::Platypus->new;
$ffi->lib(undef);
$ffi->attach(puts => ['string'] => 'int');
$ffi->attach(atan => ['double'] => 'double');
puts(4*atan(1));</lang>
{{out}}
<pre>3.14159265358979</pre>
=={{header|Perl 6}}==
{{works with|Rakudo|2018.11}}
|