7.2 Overview of the FI

7.2.2 On the C side ...

The following short piece of C code is in hello.c:

#include <stdio.h>

int hello(char *str, int num) { printf("%s %i\n", str, num); return(42 + num); }

Note that hello.c does not contain a main function -- it is instead providing a shared library, albeit a trivial one.

Compile the hello.c file using your favourite C compiler to create an object file, and from that a shared object file. A sample makefile to do this might look like this (on SunOS 4.1.3):

hello.o: hello.c
	$(CC) $(CFLAGS) -c hello.c -o hello.o

hello.so: hello.o $(LD) -Bdynamic hello.o -o hello.so

The variants for Solaris and IRIX are fairly trivial. A suitable makefile for your are available in the Foreign Interface distribution under the directory foreign/samples/.

To make use of UNIX shared libraries from MLWorks (and indeed in general) it is very important to set the LD_LIBRARY_PATH environment variable appropriately. For correct operation, the path must include the current directory (.) and the standard shared-object systems directory (usually /usr/lib). This path is very sensitive to ordering, so if you have difficulty with it, experiment with different orderings and do not rely on the documented defaults.


MLWorks Reference Manual (version 1.0) - 3 DEC 1996

Generated with Harlequin WebMaker