[Ecls-list] using built library files in C program

Juan Jose Garcia Ripoll worm at arrakis.es
Sat Apr 5 01:18:02 UTC 2003


On Friday 04 April 2003 22:56, Michael Hannemann wrote:
> Juan Jose Garcia Ripoll replies:
> >On Tuesday 01 April 2003 01:18, Michael Hannemann wrote:
> > > Following the steps in the developer's guide, it seems like I should be
> > > quite close.  I've been able to copy the stand-alone executable to
> > > another system and watch it run.  (2.5MB!  Much smaller than ACL or
> > > Lispworks.)  I've produced a .a file, and I've looked at the temporary
> > > .c and .h files.  But it seems like there's crucial interface
> > > information missing; I suspect I could call the munged and re-defined C
> > > functions L1 (L2, &c.), but is there an easier way?
> >
> >Have you considered using cl_funcall(), cl_apply() and
> > c_string_to_object()? With s = c_string_to_object("FIB") you obtain the
> > symbol for your function (assuming that the library has been initialized
> > before). With cl_funcall(2, s, MAKE_FIXNUM(2)), you use your function.
>
> If by "considered using" you mean "realized the existence of", no, I
> hadn't, but I have now.  =)  There are enough unknowns for me, though, that
> I'm going to post a little code in the hopes that you can immediately tell
> me what I'm doing wrong.
>
> >The only problem is initialization. Lisp libraries have to be initialized
> > in an order which corresponds to the order in which the would be loaded
> > if they were *.lsp files. ECL takes care of this when building executable
> > programs: build-program produces the right sequence of C statements for
> > that. If you do not want to use ECL to link the files, then I am afraid
> > you will have to inspect the output of build-program and reproduce it
> > yourself.
>
> When you say "inspect the output of build-program", do you mean at the
> level of "look at what gcc commands ECL produces", or "look at the
> init_CODE block in the temporary .h file and use that somehow"?

No, what I mean is that when you execute BUILD-PROGRAM, a small C file is 
created which initializes both your library and the lisp system.

I have appended a small modification to ecl/src/cmp/cmpmain.lsp, so that if 
you follow the example for BUILD-PROGRAM you will see what output ECL 
produces.

In a few words, what you have to do:
1) Create the example hello.lsp program (Whatever you want inside)
2) Compile hello.lsp
> (compile-file "hello.lsp" :system-p t)
3) Link everything together
> (c:build-program "myecl" :lisp-files '("hello.o"))
4) Inspect the output.

Basically, what ECL output is a sequence like

int main(int argc, char **argv) {
	/* prologue code */
	...
	/* initialize lisp and your library */
	cl_boot(argc, argv);
	read_VV(OBJNULL, init_HELLO);
	/* epilogue code */
	...
}

But I am writing this without trying it, so you'd better follow the steps I 
mentioned above :-)

BTW, libraries to be linked against standalone programs should be compiled 
with :SYSTEM-P T, in order to produce *.O object files, and not DLLs.

Juanjo

Index: cmpmain.lsp
===================================================================
RCS file: /home/jlr/cvs/ecls/src/cmp/cmpmain.lsp,v
retrieving revision 1.28
diff -c -r1.28 cmpmain.lsp
*** cmpmain.lsp	21 Mar 2003 11:24:28 -0000	1.28
--- cmpmain.lsp	5 Apr 2003 09:11:37 -0000
***************
*** 187,192 ****
--- 187,194 ----
      (ecase target
        (:program
         (setq output-name (namestring output-name))
+        (format t +lisp-program-main+ init-name prologue-code init-name
+ 	       epilogue-code)
         (with-open-file (c-file c-name :direction :output)
  	 (format c-file +lisp-program-main+ init-name prologue-code init-name
  		 epilogue-code))





More information about the ecl-devel mailing list