Call a function in a shared library: Difference between revisions

m
→‎{{header|Wren}}: Capitalize Wren & C file names.
m (→‎{{header|Wren}}: Capitalize Wren & C file names.)
 
(7 intermediate revisions by 7 users not shown)
Line 3:
Show how to call a function in a shared library (without dynamically linking to it at compile-time). In particular, show how to call the shared library function if the library is available, otherwise use an internal equivalent function.
 
This is a special case of [[Call foreign language function|calling a foreign language function]] where the focus is close to the [https://en.wikipedia.org/wiki/Application_binary_interface ABI] level and not at the normal API level.
 
 
Line 133:
dllclient.ahk
<syntaxhighlight lang="autohotkey">Msgbox, hello from client</syntaxhighlight>
=={{header|BaCon}}==
 
=={{header|BBC BASIC}}==
==={{header|BaCon}}===
<syntaxhighlight lang="qbasic">' Call a dynamic library function
PROTO j0
Line 149 ⟶ 150:
prompt$ ./calllib
0.765198</pre>
 
=={{header|BBC BASIC}}==
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
The following shared libraries are automatically available: ADVAPI32.DLL, COMCTL32.DLL, COMDLG32.DLL, GDI32.DLL, KERNEL32.DLL, SHELL32.DLL, USER32.DLL and WINMM.DLL.
<syntaxhighlight lang="bbcbasic"> SYS "MessageBox", @hwnd%, "This is a test message", 0, 0
</syntaxhighlight>
 
=={{header|C}}==
{{works with|POSIX|.1-2001}}
Line 873 ⟶ 876:
Print "Press any key to quit"
Sleep</syntaxhighlight>
 
 
=={{header|FutureBasic}}==
Use GameplayKit framework to quickly generate random integers.
<syntaxhighlight lang="futurebasic">
include "tlbx GameplayKit.incl"
 
UInt64 randomInteger
NSUInteger i
 
for i = 1 to 20
randomInteger = fn GKLinearCongruentialRandomSourceSeed( fn GKLinearCongruentialRandomSourceInit )
print randomInteger
next
 
HandleEvents
</syntaxhighlight>
 
 
=={{header|Go}}==
{{trans|C}}
Line 1,307 ⟶ 1,329:
> cfloor( 2.3 );
2.</syntaxhighlight>
 
=={{header|Lambdatalk}}==
 
Lambdatalk works in a wiki, lambdatank, hosted by any web browser coming with Javascript. Javascript has no native tools dealing with big numbers. Jonas Raoni Soares Silva has built a smart JS library, http://jsfromhell.com/classes/bignumber, which can be loaded in a wiki page, so called "lib_BN". Obviously interfaces must be built, for instance the BN.* operator multiplying two big numbers:.
 
<syntaxhighlight lang="scheme">
{script
LAMBDATALK.DICT['BN.*'] = function(){
var args = arguments[0].split(' '),
a = new BigNumber( args[0], BN_DEC ),
b = new BigNumber( args[1], BN_DEC );
return a.multiply( b )
};
</syntaxhighlight>
 
The lib_BN library can be loaded in any other wiki page via a {require lib_BN} expression and the BN.* primitive can be used this way:
 
<syntaxhighlight lang="scheme">
{BN.* 123456789123456789123456789 123456789123456789123456789}
-> 15241578780673678546105778281054720515622620750190521
 
to be compared with the "standard" lambdatalk builtin * operator
 
{* 123456789123456789123456789 123456789123456789123456789}
-> 1.524157878067368e+52
</syntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
This works on windows and on linux/mac too (through Mono)
Line 1,700 ⟶ 1,749:
cols= 96
</pre>
=={{header|RPL}}==
There is no library concept in RPL. However, in 1990, Jan Christiaan van Winkel proposed to the RPL community a way to get something close.
Assuming the programs frequently needed are stored in a specific directory named <code>MyLib</code> located at root directory, the following program, also located at the root directory, can be invoked by any program to access one of the library features.
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
PATH ➜ owd
≪ HOME MyLib RCL
1 owd SIZE '''FOR''' i
owd i GET EVAL '''NEXT'''
≫ EVAL
≫ ''''CALL'''' STO
|
'''CALL''' ''( 'Program_name' -- depending on call )''
save the old directory
push the library routine on the stack
now go back to the old directory
step by step
run the library routine
|}
{{in}}
<pre>
97 'PRIM?' CALL
</pre>
{{out}}
<pre>
1: 1
</pre>
 
=={{header|Ruby}}==
This script uses Fiddle from Ruby's standard library to open <code>fakeimglib.so</code> from the [[#C|C example]].
Line 1,802 ⟶ 1,885:
end
exit status</syntaxhighlight>
 
=={{header|Rust}}==
The standard library does not provide a way to load dynamic libraries. Without using third-party libraries, we must use the FFI to call the relevant C functions directly.
Line 1,995 ⟶ 2,079:
{{trans|C}}
An embedded program so we can ask the C host to call the shared library function for us.
<syntaxhighlight lang="ecmascriptwren">/* call_shared_library_functionCall_a_function_in_a_shared_library.wren */
 
var RTLD_LAZY = 1
Line 2,036 ⟶ 2,120:
<br>
Finally, we embed the Wren script in the following C program, compile and run it:
<syntaxhighlight lang="c">/* gcc call_shared_library_functionCall_a_function_in_a_shared_library.c -o call_shared_library_functionCall_a_function_in_a_shared_library -ldl -lwren -lm */
 
#include <stdio.h>
Line 2,135 ⟶ 2,219:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "call_shared_library_functionCall_a_function_in_a_shared_library.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
Line 2,157 ⟶ 2,241:
Same as C example depending on whether fakeimglib.so is present in the current directory or not.
</pre>
 
=={{header|X86-64 Assembly}}==
===UASM 2.52===
Line 2,294 ⟶ 2,379:
and it "just works" as all objects are "the same" whether statically or dynamically linked.
{{omit from|Batch File|Except for rundll32.exe (which is rather limited) there is no way of calling an external function}}
{{omit from|EasyLang|Libraries do not exist in EasyLang}}
{{omit from|GUISS}}
{{omit from|M4}}
9,476

edits