Call a foreign-language function: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: changed wording in the output section header.)
Line 1,980: Line 1,980:
(println 'Duplicate (duptest "Hello world!"))</lang>
(println 'Duplicate (duptest "Hello world!"))</lang>
===64-bit version===
===64-bit version===
<lang C>
<lang PicoLisp>(load "@lib/native.l")
/*

How to create the shared lib/so file:
(gcc "str" NIL
(duptest (Str) duptest 'S Str) )
gcc -c -Wall -Werror -fPIC duptest.c
gcc -shared -o duptest.so duptest.o -Wno-undef
*/


#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>


char *duptest(char *str) {
extern char * duptest(char * str);
static char *s;


char * duptest(char * str) {
static char * s;
free(s); // We simply dispose the result of the last call
free(s); // We simply dispose the result of the last call
return s = strdup(str);
return s = strdup(str);
}
}
/**/


int main() {
(println 'Duplicate (duptest "Hello world!"))</lang>
}
</lang>

<lang PicoLisp>
(prinl "Calling custom so/dll library...")
(set 'A NIL)
(set 'A (native "./duptest.so" "duptest" 'S "abc"))
(prinl "A=" A)
(when (not (= A NIL)) (prinl "Success!"))
</lang>


<out>
Output in both cases:
<pre>
<pre>Duplicate "Hello world!"</pre>
Calling custom so/dll library...
A=abc
Success!
</pre>
</out>


=={{header|PL/I}}==
=={{header|PL/I}}==