Compiling with ECL (1): File types
From this post, I will introduce topics on compiling Lisp programs
with ECL, Embeddable
Overview
You can generate following files with ECL.
- Object file (.o)
- Fasl file (.fas)
- Static library (.a)
- Shared library (.so)
- Executable
file
Relations among them are depicted below:
Object file
As you can see, object file works as an intermedia:system-p t
option, which generates object files.
On linux systems, ECL invokes gcc -c
for generating
An object file consists of some functions in C:
- Functions correspond
ing to Lisp functions - The initializa
tion function which registers defined functions on the lisp environmen t
Consider the example below.
1 (defun say-hello () 2 (print "Hello, world"))
During compilatio
static cl_object L1say_hell
: correspondo ing to 'say-hello' function ECL_DLLEXP
: initializaORT void _eclwm2nNa uJEfEnD_CL Sxi0z(cl_object flag) tion function
In order to use these object files from your C program, you have to call initializa
Fasl file
If you want to make a library which is loaded dynamicall
In current ECL(9.10.2), a fasl file is just a shared library file. This means you can load fasl files with dlopen
and initializeload
lisp function.
Creating a fasl file from one lisp file is very easy.
1 (compile-file "hello.lsp")
To create a fasl file from some lisp files, firstly you have to compile each lisp file into object file, and then combine them with c:build-fa
.
1 ;; generates hello.o 2 (compile-file "hello.lsp" :system-p t) 3 ;; generates goodbye.o 4 (compile-fi le "goodbye.ls p" :system-p t) 5 6 ;; generates hello-good bye.fas 7 (c:build-fa sl "hello-good bye" 8 :lisp-file s '("hello.o" "goodbye.o"))
Static library
ECL can compile lisp programs to static libraries, which can be linked with
C programs. A static library is created by c:build-st
with some compiled object files.
1 ;; generates hello.o 2 (compile-file "hello.lsp" :system-p t) 3 ;; generates goodbye.o 4 (compile-fi le "goodbye.ls p" :system-p t) 5 6 ;; generates libhello-g oodbye.a 7 (c:build-st atic-libra ry "hello-good bye" 8 :lisp-file s '("hello.o" "goodbye.o") 9 :init-name "init_hello _goodbye")
When you use static/sha:init-name
option. In this example, "init_hello
1 #include <ecl/ecl.h> 2 extern void init_hello_goodbye(cl_object cblock); 3 4 int 5 main(int argc, char **argv) 6 { 7 // setup the lisp runtime 8 cl_boot(argc, argv); 9 10 // call the init function via read_VV 11 read_VV(OBJNULL, init_hello _goodbye); 12 13 ... 14 15 // shutdown the lisp runtime 16 cl_shutdow n(); 17 18 return 0; 19 }
Because the program itself does not know the type of the init
function, a prototype declaratioinit_hello
via
read_VV
. init_hello
takes a argument, and read_VV
supplies an appropriat
Shared library
Almost the same as the case of static library.
Executable file
Some of you might want to create a standalonec:build-pr
does
the job.
1 ;; generates hello.o 2 (compile-file "hello.lsp" :system-p t) 3 ;; generates goodbye.o 4 (compile-fi le "goodbye.ls p" :system-p t) 5 6 ;; generates hello-good bye 7 (c:build-pr ogram "hello-good bye" 8 :lisp-file s '("hello.o" "goodbye.o"))
Summary
In this post, some file types that can be compiled to with ECL were introduced
- Object file: intermedia
te file format for others - Fasl file: loaded dynamicall
y via load
lisp function - Static library: linked with and used from C programs
- Shared library: loaded dynamicall
y and used from C programs - Executable
: standalone executable
ECL provides a high-levelc:build-*
for each format. If you want to use them, you can find detailed descriptio