4.2 Compiling and loading
It is strongly recommended to copy the makefile "Makefile.external"
provided in your installation directory under lib/$ARCH and adapt it
for your purposes.
If the makefile is not used, the command to compile a C source
with ECLiPSe library calls looks something like this:
% cc -G -I/usr/local/eclipse/include/sparc_sunos5
-o eg_externals.so eg_externals.c
or
% cc -shared -I/usr/local/eclipse/include/i386_linux
-o eg_externals.so eg_externals.c
If the external is to be used in a standalone ECLiPSe,
it is possible to dynamically load it
using the load/1
predicate:
load("eg_externals.so")
On older UNIX platforms without dynamic loading, the following method
may work. Compile the source using
% cc -c -I/usr/local/eclipse/include/sparc_sunos5 eg_externals.c
and load it with a command like
load("eg_externals.o -lg -lm")
The details may vary depending on what compiler and operating system
you use. Refer to the Makefile.external for details.
Once the object file containing the C function has been loaded into
ECLiPSe, the link between the function and a predicate name
is made with
external/2
external(sumlist/2, p_sumlist)
The new predicate can now be called like other predicates.
Note that the
external/2
declaration must precede any call to the declared predicate,
otherwise the ECLiPSe compiler will issue an inconsistent
redefinition error. Alternatively, the
external/1
forward declaration can be used to prevent this.
If the external is needed in the context of an ECLiPSe which is itself
embedded in a C/C++ host program, then the external code can be
compiled/linked together with the host program, and the link between function
and predicate name can alternatively be made by calling the C function
ec_external(), e.g.
ec_external(ec_did("sumlist",2), p_sumlist, ec_did("eclipse"))
This must be done after the embedded ECLiPSe has been initialised
(and after the module that is supposed to contain the external predicate
has already been created).