[Ecls-list] Embedding ECL - how?

Juan Jose Garcia-Ripoll Juan.Ripoll at mpq.mpg.de
Sun Jan 20 09:38:04 UTC 2002


"Dr. Edmund Weitz" wrote:
> 
> I understand that ECL was originally designed for being embeddable
> into C based applications.

Historically it was not. At some point G. Attardi wrote a version of ECL
that could be embedded into C programs. There is a paper somewhere but I
lost the link. The way ECL currently works is more on the line of
embedding all your C code into the lisp program. What ECL offers since a
few months ago is the possibility to write your own toplevel in C, so
that it is your C code the one that takes control, and not the lisp
interpreter.

> However, I wonder how this is to be
> done. Am I supposed to link with libecl.a and the other libraries in
> the same directory? What next? Do I have to start the REPL or can I
> link with or load object files that I have created with COMPILE-FILE
> and start my own functions directly somehow? Are examples available
> somewhere? I've read the developer's guide but I didn't find any
> pointers.

I have had problems lately that prevent me from working on ECL on my
spare time -- lack of computer at home, which means I had to dedicate my
energy to other things such as building up these IKEA furnitures :)

Regarding the example, I can write a sketch of what you should do, but I
cannot check it.

1) Get a working ECL image.

2) Write your C/C++ code and make a few libraries with it (static or
dynamically linked, doesn't mind)

3) Decide what your "int main()" routine should look like. Basically it
is going to look as follows

	int main(int argc, char **argv) {
	  /* a) prologue code */
	  ...
	  /* b) initialization of lisp library */
	  cl_boot(argc, argv);
	  ...
	  /* c) epilogue code */
	  ...
        }

3.a) The default prologue does nothing, but it is intended for
customization to be performed by your C code before the lisp gets in the
way.

3.b) The boot code (cl_boot() and other things) are inserted by ECL and
you cannot decide what they will look like. It basically sets up the
garbage collector and loads the core libraries.

3.c) The epilogue code is the useful parts and tells what should be done
once everything is set up. The default epilogue is a call to the
toplevel function

	/* c) epilogue code */
	funcall(1,_intern(\"TOP-LEVEL\",system_package));
        return 0;

but it can be anything you want.

4) Tell ECL to link your program. Say that your C library is "myclib.a"
and that you want to call myc_init() at the epilogue and that you want
to transfer control to some routine myc_toplevel() during the epilogue.
Then enter a ECL image and type

	> (load "sys:cmp")
	[... loads the compiler ...]
	> (c::build-program "myimage" :ld-flags '("-lmyclib")
	    :prologue-code "myc_init(argc, argv);"
	    :epilogue-code "myc_toplevel(); return 0;")

This will produce an executable called "myimage" or "myimage.exe" which
first calls myc_init() passing the command line arguments, initializes
the lisp core and finally transfers control to your C program.

Hope this helps. This tutorial should be improved and accompanied with
useful examples but I cannot do that for the moment.

Juanjo




More information about the ecl-devel mailing list