[Ecls-list] calling ECL functions defined in an ASDF package from C code

Eric Schulte schulte.eric at gmail.com
Sun Jun 26 20:49:58 UTC 2011


Juan Jose Garcia-Ripoll <juanjose.garciaripoll at googlemail.com> writes:

> On Sun, Jun 26, 2011 at 8:49 PM, Eric Schulte <schulte.eric at gmail.com>wrote:
>
>>
>> - What are the contents of main.c in your example above?  I assume it is
>>  hand written and not generated by ecl?
>>
>> - Were you required to write a libfoo.h file to include in main.c or is
>>  that not necessary?
>
>
> Sorry, I did not copy-paste the content of the C file. Here it goes:
>
> $ ecl -norc
> ECL (Embeddable Common-Lisp) 11.1.1
> 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.
>> (require :cmp)
>
> ;;; Loading #P"/Users/jjgarcia/lib/ecl-11.1.1/cmp.fas"
> ("CMP")
>
>> (setf c::*compile-in-constants* t)
>
> T
>> (c:build-shared-library "foo" :lisp-files (list (compile-file "foo"
> :system-p t :verbose nil)))
> ;;; [...]
> ;;; Note:
> ;;;   Library initialization function is main_dll_FOO
> ;;; [...]
> #P"libfoo.dylib"
>> (ext:system "gcc -o main.exe main.c -L$HOME/lib -L./ -lfoo -lecl")
>
> 0
>> (ext:system "./main.exe")
>
> HOLA
> 0
>> (ext:system "cat ./foo.lsp")
> (defun foo (x)
>  (print x))
>
> (foo 'hola)
>
> 0
>> (ext:system "cat ./main.c")
> extern int main_dll_FOO(int argc, char **argv);
>
> int main(int argc, char **argv)
> {
>   main_dll_FOO(argc, argv);
>   si_exit(0);
> }
>
> 0
>
> Notice that the name of the function was announced by ECL in a comment after
> C:BUILD-SHARED-LIBRARY. The declaration does not need to live in a *.h file,
> it may be in the same compiled file.
>
> Juanjo

Beautiful, this is now working on my system.  The only change I needed
to make was setting LD_LIBRARY_PATH so that libfoo.so could be found at
runtime, with the following

  (ext:system "export LD_LIBRARY_PATH=/home/eschulte/Desktop/lisp-lib; ./main.exe")

Thanks -- Eric

-- 
Eric Schulte
http://cs.unm.edu/~eschulte/




More information about the ecl-devel mailing list