Sophie

Sophie

distrib > Fedora > 14 > x86_64 > by-pkgid > e3d62627d1d1aab7ab1be2dd7f65a872 > files > 295

ecl-10.4.1-1.fc14.x86_64.rpm

<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>1.6.&#160;Compiler examples</title><link rel="stylesheet" href="ecl.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.75.2"><link rel="home" href="index.html" title="The ECL manual"><link rel="up" href="ch24.html" title="Chapter&#160;1.&#160;Building programs"><link rel="prev" href="ch24s05.html" title="1.5.&#160;File names"><link rel="next" href="ch25.html" title="Chapter&#160;2.&#160;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.&#160;Compiler examples</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch24s05.html">Prev</a>&#160;</td><th width="60%" align="center">Chapter&#160;1.&#160;Building programs</th><td width="20%" align="right">&#160;<a accesskey="n" href="ch25.html">Next</a></td></tr></table><hr></div><div class="section" title="1.6.&#160;Compiler examples"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="Internals-Compiler-examples"></a>1.6.&#160;Compiler examples</h2></div></div></div><div class="section" title="1.6.1.&#160;The hello.lisp file"><div class="titlepage"><div><div><h3 class="title"><a name="id675955"></a>1.6.1.&#160;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.
   &gt; <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.&#160;Example of loadable object file"><div class="titlepage"><div><div><h3 class="title"><a name="id676016"></a>1.6.2.&#160;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.
   &gt; <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.
    &gt; <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,&#8211;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.
    &gt; <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.&#160;Example of standalone program"><div class="titlepage"><div><div><h3 class="title"><a name="id676095"></a>1.6.3.&#160;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.
    &gt; <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">
   &gt; <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,&#8211;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.&#160;Combining files into a larger FASL"><div class="titlepage"><div><div><h3 class="title"><a name="id676201"></a>1.6.4.&#160;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.
    &gt; (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">
   &gt; (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,&#8211;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">
    &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; THIS EXAMPLE IS WRONG?! &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;
    &gt; (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>&#160;</td><td width="20%" align="center"><a accesskey="u" href="ch24.html">Up</a></td><td width="40%" align="right">&#160;<a accesskey="n" href="ch25.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">1.5.&#160;File names&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;Chapter&#160;2.&#160;Manipulating Lisp objects</td></tr></table></div></body></html>