Show how a foreign language function can be called from the language.
As an example, consider calling functions defined in the C language. Create a string containing “Hello World!” of the string type typical to the language. Pass the string content to C‘s
strdup. The content can be copied if necessary. Get the result from
strdup and print it using language means. Do not forget to free the result of
strdup (allocated in the heap).
- It is not mandated if the C run-time library is to be loaded statically or dynamically. You are free to use either way.
- C++ and C solutions can take some other language to communicate with.
- It is not mandatory to use
strdup, especially if the foreign function interface being demonstrated makes that uninformative.
While calling C functions from C++ is generally almost trivial,
strdup illustrates some fine point in communicating with C libraries. However, to illustrate how to generally use C functions, a C function
strdup1 is used, which is assumed to have the same interface and behaviour as strdup, but cannot be found in a standard header.
In addition, this code demonstrates a call to a FORTRAN function defined as
FUNCTION MULTIPLY(X, Y) DOUBLE PRECISION MULTIPLY, X, Y
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.
Content is available under GNU Free Documentation License 1.2.