<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>1.6. Compiler examples</title><link rel="stylesheet" type="text/css" href="ecl.css"><meta name="generator" content="DocBook XSL Stylesheets V1.76.1"><link rel="home" href="index.html" title="The ECL manual"><link rel="up" href="ch24.html" title="Chapter 1. Building programs"><link rel="prev" href="ch24s05.html" title="1.5. File names"><link rel="next" href="ch25.html" title="Chapter 2. Manipulating Lisp objects"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">1.6. Compiler examples</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch24s05.html">Prev</a> </td><th width="60%" align="center">Chapter 1. Building programs</th><td width="20%" align="right"> <a accesskey="n" href="ch25.html">Next</a></td></tr></table><hr></div><div class="section" title="1.6. Compiler examples"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="Internals-Compiler-examples"></a>1.6. Compiler examples</h2></div></div></div><div class="section" title="1.6.1. The hello.lisp file"><div class="titlepage"><div><div><h3 class="title"><a name="id2993015"></a>1.6.1. The <code class="filename">hello.lisp</code> file</h3></div></div></div><p>In the following examples we will use the same lisp program. You have to create a file called <code class="filename">hello.lisp</code> which contains the following lines</p><pre class="programlisting"> (princ "Hello world!") (terpri) (quit) </pre><p class="continues">If you start <span class="application">ECL</span> and load this file in the Common-Lisp environment you will see the <code class="literal">"Hello world!"</code> message and the interpreter will be closed.</p><pre class="screen"> 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. > <em class="lineannotation"><span class="lineannotation">(load "hello.lisp")</span></em> ;;; Loading "hello.lisp" Hello World! </pre></div><div class="section" title="1.6.2. Example of loadable object file"><div class="titlepage"><div><div><h3 class="title"><a name="id2993086"></a>1.6.2. Example of loadable object file</h3></div></div></div><p>You can only perform the example in this section if your <span class="application">ECL</span> image supports dynamically loading of object files. This is true if you find the keyword <em class="replaceable"><code>:dlopen</code></em> in the <em class="replaceable"><code>*features*</code></em> variable. This is true, for instance, in a typical FreeBSD or Linux box,</p><pre class="screen"> Type :h for Help. Top level. > <em class="lineannotation"><span class="lineannotation">*features*</span></em> (:IEEE-FLOATING-POINT :IBM-PC :I386 :BSD :UNIX :DLOPEN :ANSI-CL :CLOS :BOEHM-GC :ECL :COMMON) </pre><p>In this example we build a loadable extension which prints the <code class="literal">"Hello world!"</code> message. First you need to create a the <code class="filename">hello.lisp</code> file. Next you have to enter the <span class="application">ECL</span> environment and type <code class="literal">(compile-file "hello.lisp")</code>. This produces a loadable object file.</p><pre class="screen"> Type :h for Help. Top level. > <em class="lineannotation"><span class="lineannotation">(compile-file "hello.lisp")</span></em> ;;; 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. > <em class="lineannotation"><span class="lineannotation">(load "hello")</span></em> ;;; Loading #P"hello.fas" Hello World! </pre></div><div class="section" title="1.6.3. Example of standalone program"><div class="titlepage"><div><div><h3 class="title"><a name="id2993192"></a>1.6.3. Example of standalone program</h3></div></div></div><p>In this example we build a standalone program which prints the <code class="literal">"Hello world!"</code> message and does nothing else. First you must create the <code class="filename">hello.lisp</code> file shown above. Next you have to enter the <span class="application">ECL</span> environment and type <code class="literal">(compile-file "hello.lisp" :system-p t)</code>. This produces an object file that can be linked against the <span class="application">ECL</span> core image.</p><pre class="screen"> Type :h for Help. Top level. > <em class="lineannotation"><span class="lineannotation">(compile-file "hello.lisp" :system-p t)</span></em> ;;; 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" </pre><p class="continues">The final step is to build the executable using the <code class="literal">c:build-program</code> instruction.</p><pre class="screen"> > <em class="lineannotation"><span class="lineannotation">(c:build-program "myecl" :lisp-files '("hello.o"))</span></em> ;;; 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. </pre><p class="continues">Now you can execute this program from your favorite shell.</p><pre class="screen"> % <em class="lineannotation"><span class="lineannotation">./myecl</span></em> Hello world! </pre></div><div class="section" title="1.6.4. Combining files into a larger FASL"><div class="titlepage"><div><div><h3 class="title"><a name="id2993300"></a>1.6.4. Combining files into a larger FASL</h3></div></div></div><p>You can only perform the example in this section if your <span class="application">ECL</span> image supports dynamically loading of object files. In this example we build a loadable library which prints the <code class="literal">"Hello world!"</code> message and does nothing else. First you must create the <code class="filename">hello.lisp</code> file shown above. Next you have to enter the <span class="application">ECL</span> environment and type <code class="literal">(compile-file "hello.lisp" :system-p t)</code>. This produces an object file that can be linked to form a loadable library.</p><pre class="screen"> 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" </pre><p class="continues">The final step is to build the library using the <code class="literal">c:build-fasl</code> instruction.</p><pre class="screen"> > (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" </pre><p class="continues">Now you can load this extension from any <span class="application">ECL</span> image, even those you produce with <code class="literal">c:build-program</code>.</p><pre class="screen"> <<<<<<<< THIS EXAMPLE IS WRONG?! >>>>>>>>> > (load "myecl") ;;; Loading myecl.fas Hello world! Bye. </pre></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch24s05.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch24.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch25.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">1.5. File names </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 2. Manipulating Lisp objects</td></tr></table></div></body></html>