<div class="gmail_quote">On Thu, Jul 28, 2011 at 12:50 AM, Mark McCurry <span dir="ltr"><<a href="mailto:mark.d.mccurry@gmail.com">mark.d.mccurry@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div class="im">Well, I had no luck digging through the mailing list and the wiki is vague.</div>
I have managed to get the linking working after playing around for a bit.<br>
The process I used to link the static lisp code "F-NAME.lisp" was:<br>
<br>
1) Run ecl script to:<br>
(compile-file F-NAME.lisp :system-p t)<br>
(c:build-static-library F-NAME :lisp-files '(F-NAME.o))<br>
<br>
2) In C file to be linked:<br>
cl_boot(argc, argv);<br>
void init_lib_F_NAME(cl_object);<br>
read_VV(OBJNULL, init_lib_F_NAME);<br>
cl_funcall(...);<br>
<br>
3) Compile C with static library<br>
<br>
Is this the 'right' way to embed a static piece of lisp code into a C program?</blockquote><div><br></div><div>There is no single right way to do it. Possibilities are many and I would favor some that many people do not like.</div>

<div><br></div><div><b>TYPE I: ECL in control</b></div><div><br></div></div>* Compile all your C code into a statically or dynamically linked library, say libc_extensions.a.<div><br><div>* Organize your program in the lisp part, including the lisp code that is to be executed at startup. Say the "startup" function is MY-PACKAGE:ENTRY-POINT</div>

<div><br></div><div>* Compile each file using the flag :SYSTEM-P T and link all the resulting files into a program using C:BUILD-PROGRAM</div><div><br></div><div><div><font class="Apple-style-span" face="'courier new', monospace">   (let ((list-of-files '("mylisp1.lisp" "mylisp2.lisp" ... ))</font></div>

<div><font class="Apple-style-span" face="'courier new', monospace">         (compiled-files (mapcar (lambda (x) (compile-file x :system-p t))</font></div><div><font class="Apple-style-span" face="'courier new', monospace">                                 list-of-files))</font></div>

<div><font class="Apple-style-span" face="'courier new', monospace">     (c:build-static-library "lisp_extension"</font></div><div><font class="Apple-style-span" face="'courier new', monospace">                             :lisp-files compiled-files</font></div>

<div><font class="Apple-style-span" face="'courier new', monospace">                             :ld-flags '("-lc_extension")</font></div><div><font class="Apple-style-span" face="'courier new', monospace">                             :epilogue-code '(MY-PACKAGE:ENTRY-POINT)</font></div>

<div><font class="Apple-style-span" face="'courier new', monospace">     ))</font></div></div><div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div>I very much prefer this way because it hides from you the link flags and also the initialization of ECL. It is also much simpler to capture and handle exceptions from lisp code than from C.</div>

<div><br></div><div>Note that you need not use :epilogue-code or the function MY-PACKAGE;ENTRY-POINT, because on startup _all_ lisp code will be executed sequentially.</div><div><br></div><div><b>TYPE II: Your code in control</b></div>

<div><br></div><div>* Organize your lisp code in a set of lisp files, with certain public functions that are to be called from C perfectly identified by name.</div><div><br></div><div>* Compile and build everything into a statically or dynamically linked library (that itself depends on libecl.so or ecl.dll!)</div>

<div><br></div><div><font class="Apple-style-span" face="'courier new', monospace">   (let ((list-of-files '("mylisp1.lisp" "mylisp2.lisp" ... ))</font></div><div><font class="Apple-style-span" face="'courier new', monospace">         (compiled-files (mapcar (lambda (x) (compile-file x :system-p t))</font></div>

<div><font class="Apple-style-span" face="'courier new', monospace">                                 list-of-files))</font></div><div><font class="Apple-style-span" face="'courier new', monospace">     (c:build-static-library "lisp_extension"</font></div>

<div><font class="Apple-style-span" face="'courier new', monospace">                             :lisp-files compiled-files))</font></div><div><br></div><div>* Compile your C program as usual and add your lisp library as a dependency. In this case you would have to add -llisp_extension and -lecl when linking the program and you have to code your main program to initialize the lisp library</div>

<div><br></div><div><font class="Apple-style-span" face="'courier new', monospace">int main(int argc, char **argv)</font></div><div><font class="Apple-style-span" face="'courier new', monospace">{<br></font></div>

<div><font class="Apple-style-span" face="'courier new', monospace">  cl_boot(argc, argv);</font></div><div><font class="Apple-style-span" face="'courier new', monospace">  extern cl_object init_LIB_LISP_EXTENSION(cl_object);</font></div>

<div><font class="Apple-style-span" face="'courier new', monospace">  read_VV(OBJNULL, init_LIB_LISP_EXTENSION);</font></div><div><font class="Apple-style-span" face="'courier new', monospace">  ...</font></div>

<div><font class="Apple-style-span" face="'courier new', monospace">}</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div>This is more or less what you did but you have to make sure that both your lisp code and ECL's library is included in the dependencies of your program, otherwise it will fail to link. I do not like it very much, except for very large projects where somehow ECL has to be integrated by hand. It has the severe disadvantage that you have to know how to create lisp objects, find lisp symbols and call lisp functions in C, and use some interfaces (read_VV) which may change in the near future.<br clear="all">

<br></div><div>There are detailed examples about how to build libraries and programs using ECL as a linker and including external C code in examples/build and examples/asdf (they have been updated in CVS because some files had disappeared) When organizing and building your code I very much recommend using ASDF and ASDF:MAKE-BUILD (<a href="http://ecls.sourceforge.net/new-manual/ch16.html">http://ecls.sourceforge.net/new-manual/ch16.html</a>) instead of compiling your files manually.</div>

<div><br></div><div>Juanjo</div><div><br>-- <br>Instituto de Física Fundamental, CSIC<br>c/ Serrano, 113b, Madrid 28006 (Spain) <br><a href="http://juanjose.garciaripoll.googlepages.com" target="_blank">http://juanjose.garciaripoll.googlepages.com</a><br>


</div></div>