Call a foreign-language function: Difference between revisions

m
Fixed lang tags.
(→‎{{header|Python}}: add ctypes example)
m (Fixed lang tags.)
Line 14:
=={{header|Ada}}==
Ada provides standard interfaces to [[C]], [[C++]], [[Fortran]] and [[Cobol]]. Other language interfaces can be provided as well, but are not mandatory. Usually it is possible to communicate to any language that supports calling conventions standard to the [[OS]] ('''cdecl''', '''stdcall''' etc).
<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
<lang Ada>
with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C; use Interfaces.C;
with Interfaces.C.Strings; use Interfaces.C.Strings;
Line 29 ⟶ 28:
Put_Line (Value (S2));
Free (S2);
end Test_C_Interface;</lang>
</lang>
 
=={{header|C++}}==
Line 36 ⟶ 34:
 
In addition, this code demonstrates a call to a FORTRAN function defined as
<lang cpp>FUNCTION MULTIPLY(X, Y)
<lang fortran>
DOUBLE PRECISION MULTIPLY, X, Y</lang>
FUNCTION MULTIPLY(X, Y)
DOUBLE PRECISION MULTIPLY, X, Y
</lang>
Note that the calling convention of FORTRAN depends on the system and the used FORTRAN compiler, and sometimes even on the command line options used for the compiler; here, GNU Fortran with no options is assumed.
<lang cpp>#include <cstdlib> // for C memory management
<lang cpp>
#include <cstdlib> // for C memory management
#include <string> // for C++ strings
#include <iostream> // for output
Line 83 ⟶ 78:
// free, not delete, nor delete[], nor operator delete
std::free(msg2);
}</lang Ada>
}
</lang>
 
=={{header|Common Lisp}}==
Line 103 ⟶ 97:
Every version of GNU Forth has experimented with a different means to do C foreign function calls. The current implementation resolves various incompatibilities which had plagued earlier mechanisms by parsing C header files and using the host's native toolchain (i.e. gcc and ld) to generate thunks.
 
<lang forth>c-library cstrings
c-library cstrings
 
\c #include <string.h>
Line 126 ⟶ 119:
duped dup strlen type \ testing
 
duped free throw \ gforth ALLOCATE and FREE map directly to C's malloc() and free()</lang>
</lang>
 
=={{header|Lisaac}}==
Use backtick notation (`...`) for referencing foreign language (C) features.
<lang Lisaac>Section Header
Section Header
 
+ name := TEST_C_INTERFACE;
Line 155 ⟶ 146:
// this will also be inserted in-place, expression type disregarded
`free(@p)`;
);</lang>
</lang>
 
=={{header|Modula-3}}==
Line 186 ⟶ 176:
===Outline of what is linked against===
For the hypothetical [[C]] library that contains functions described by a header file with this in:
<lang cocaml>void myfunc_a();
float myfunc_b(int, float);
char *myfunc_c(int *);</lang>
Line 200 ⟶ 190:
 
====file "wrap_mylib.c":====
<lang Cocaml>#include <caml/mlvalues.h>
#include <caml/alloc.h>
#include <mylib.h>
Line 232 ⟶ 222:
 
====the Makefile:====
<lang Makefileocaml>wrap_mylib.o: wrap_mylib.c
ocamlc -c -ccopt -I/usr/include/mylib $<
 
Line 263 ⟶ 253:
=={{header|Python}}==
 
<lang python>import ctypes
import ctypes
libc = ctypes.CDLL("/lib/libc.so.6")
libc.strcmp("abc", "def") # -1
libc.strcmp("hello", "hello") # 0</lang>
</lang>
 
=={{header|Ruby}}==
Anonymous user