Previous: File names, Up: Building programs



2.6 Compiler examples

2.6.1 The hello.lisp file

In the following examples we will use the same lisp program. You have to create a file called hello.lisp which contains the following lines

     (princ "Hello world!")
     (terpri)
     (quit)

If you start ECL and load this file in the Common-Lisp environment you will see the "Hello world!" message and the interpreter will be closed.

ECL (Embeddable Common-Lisp) 0.9d
Copyright (C) 1984 Taiichi Yuasa and Masami Hagiya
Copyright (C) 1993 Giuseppe Attardi
Copyright (C) 2000 Juan J. Garcia-Ripoll
        ECL is free software, and you are welcome to redistribute it
under certain conditions; see file 'Copyright' for details.
Type :h for Help.  Top level.
> (load "hello.lisp")
;;; Loading "hello.lisp"
Hello World!

2.6.2 Example of loadable object file

You can only perform the example in this section if your ECL image supports dynamically loading of object files. This is true if you find the keyword :dlopen in the *features* variable. This is true, for instance, in a typical FreeBSD or Linux box,

Type :h for Help.  Top level.
> *features*
(:IEEE-FLOATING-POINT :IBM-PC :I386 :BSD :UNIX :DLOPEN :ANSI-CL :CLOS
    :BOEHM-GC :ECL :COMMON)

In this example we build a loadable extension which prints the "Hello world!" message. First you need to create a the hello.lisp file. Next you have to enter the ECL environment and type (compile-file "hello.lisp"). This produces a loadable object file.

Type :h for Help.  Top level.
> (compile-file "hello.lisp")
;;; Loading #P"/usr/lib/ecl/cmp.fas"
;;; Loading #P"/usr/lib/ecl/sysfun.lsp"
;;; Compiling hello.lisp.
;;; End of Pass 1.
;;; Calling the C compiler...
;;; Invoking external command: gcc -O2 -march=i686 -pipe -fomit-frame-pointer -fPIC -fstrict-aliasing -Dlinux -O "-I/usr/lib/ecl/" -w -c "hello.c" -o "hello.o"
;;; Invoking external command: gcc -o "hello.fas" -L"/usr/lib/ecl/" "hello.o"  -Wl,–rpath,/usr/lib/ecl/ -shared   -lecl -lgmp -lgc -ldl -lm
;;; OPTIMIZE levels: Safety=2, Space=0, Speed=3
;;; Finished compiling hello.lisp.
#P"hello.fas"
Top level.
> (load "hello")
;;; Loading #P"hello.fas"
Hello World!

2.6.3 Example of standalone program

In this example we build a standalone program which prints the "Hello world!" message and does nothing else. First you must create the hello.lisp file shown above. Next you have to enter the ECL environment and type (compile-file "hello.lisp" :system-p t). This produces an object file that can be linked against the ECL core image.

Type :h for Help.  Top level.
> (compile-file "hello.lisp" :system-p t)
;;; Loading #P"/usr/lib/ecl/cmp.fas"
;;; Loading #P"/usr/lib/ecl/sysfun.lsp"
;;; Compiling hello.lisp.
;;; End of Pass 1.
;;; Calling the C compiler...
;;; Invoking external command: gcc -O2 -march=i686 -pipe -fomit-frame-pointer -fPIC -fstrict-aliasing -Dlinux -O "-I/usr/lib/ecl/" -w -c "hello.c" -o "hello.o"
;;; OPTIMIZE levels: Safety=2, Space=0, Speed=3
;;; Finished compiling hello.lisp.
#P"hello.o"

The final step is to build the executable using the c:build-program instruction.

> (c:build-program "myecl" :lisp-files '("hello.o"))
;;; Invoking external command: gcc -O2 -march=i686 -pipe -fomit-frame-pointer -fPIC -fstrict-aliasing -Dlinux -O "-I/usr/lib/ecl/" -w -c "myecl.c" -o "myecl.o"
;;; Invoking external command: gcc -o "myecl" -L"/usr/lib/ecl/" "myecl.o" "hello.o"  -Wl,–rpath,/usr/lib/ecl/  -lecl -lgmp -lgc -ldl -lm
#P"myecl"
Top level.

Now you can execute this program from your favorite shell.

% ./myecl
Hello world!

2.6.4 Combining files into a larger FASL

You can only perform the example in this section if your ECL image supports dynamically loading of object files. In this example we build a loadable library which prints the "Hello world!" message and does nothing else. First you must create the hello.lisp file shown above. Next you have to enter the ECL environment and type (compile-file "hello.lisp" :system-p t). This produces an object file that can be linked to form a loadable library.

Type :h for Help.  Top level.
> (compile-file "hello.lisp" :system-p t)
;;; Loading #P"/usr/lib/ecl/cmp.fas"
;;; Loading #P"/usr/lib/ecl/sysfun.lsp"
;;; Compiling hello.lisp.
;;; End of Pass 1.
;;; Calling the C compiler...
;;; Invoking external command: gcc -O2 -march=i686 -pipe -fomit-frame-pointer -fPIC -fstrict-aliasing -Dlinux -O "-I/usr/lib/ecl/" -w -c "hello.c" -o "hello.o"
;;; OPTIMIZE levels: Safety=2, Space=0, Speed=3
;;; Finished compiling hello.lisp.
#P"hello.o"

The final step is to build the library using the c:build-fasl instruction.

> (c:build-fasl "myecl" :lisp-files '("hello.o"))
;;; Invoking external command: gcc -O2 -march=i686 -pipe -fomit-frame-pointer -fPIC -fstrict-aliasing -Dlinux -O "-I/usr/lib/ecl/" -w -c "myecl.c" -o "myecl.o"
;;; Invoking external command: gcc -o "libmyecl.so" -L"/usr/lib/ecl/" "myecl.o" "hello.o"  -Wl,–rpath,/usr/lib/ecl/ -shared   -lecl -lgmp -lgc -ldl -lm
#P"libmyecl.so"

Now you can load this extension from any ECL image, even those you produce with c:build-program.

<<<<<<<< THIS EXAMPLE IS WRONG?! >>>>>>>>>
> (load "myecl")
;;; Loading myecl.fas
Hello world!
Bye.