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

Eric Schulte schulte.eric at gmail.com
Sun Jun 26 18:49:32 UTC 2011


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

> On Sun, Jun 26, 2011 at 7:11 AM, Eric Schulte <schulte.eric at gmail.com>wrote:
>
>> attempting to compile this program results in the following error message
>>
>>    $ gcc -o use -L. -lecl -lfoo
>>    /usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/../../../crt1.o: In
>> function `_start':
>>    [.text+0x20]: undefined reference to `main'
>>    collect2: ld returned 1 exit status
>>
>
> The order of libraries is wrong and this is missing a C file (use.c) that
> calls the library.  This should have read
>
> gcc -o use use.c -L. -lfoo -lecl
>
> I have uploaded a fixed version of ECL that adds a warning about the files
> that one tries to link into a standalone DLL. It follows a real session of
> how this works right now
>
> $ 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")
>> (c::build-shared-library "foo" :lisp-files (list (compile-file "foo"
> :system-p t :verbose nil)))
>
> ;;; Compiling (DEFUN FOO ...).
> ;;; Emitting code for FOO.
> ;;; Note:
> ;;;   Invoking external command:
> ;;;   gcc -I. -I/Users/jjgarcia/include/ -g -O2 -fPIC -fno-common -Ddarwin
> -O2 -w -c foo.c -o foo.o
> "common-lisp-user::foo common-lisp-user::hola 0"
> Condition of type: SIMPLE-ERROR
> In C:BUILDER, when building file
>  libfoo.dylib
> tried to link together files that contained split binary data.
> Unfortunately this is currently not possible. To avoid this
> recompile the files setting C::*COMPILE-IN-CONSTANTS* to T.
> List of offending files:
>  #P"/Users/jjgarcia/foo.o"
>
> Available restarts:
>
> 1. (RESTART-TOPLEVEL) Go back to Top-Level REPL.
>
> Broken at SI:BYTECODES. [Evaluation of: (C:BUILD-SHARED-LIBRARY "foo"
> :LISP-FILES (LIST (COMPILE-FILE "foo" :SYSTEM-P T :VERBOSE NIL)))]
>>> :q
>
> Top level.
>> (setf c::*compile-in-constants* t)
>
> T
>> (c::build-shared-library "foo" :lisp-files (list (compile-file "foo"
> :system-p t :verbose nil)))
> ;;; Compiling (DEFUN FOO ...).
> ;;; Emitting code for FOO.
> ;;; [...]
> ;;; Note:
> ;;;   Library initialization function is main_dll_FOO
> ;;; [...]
> #P"libfoo.dylib"
>> (si::system "gcc -o main.exe main.c -L$HOME/lib -L./ -lfoo -lecl")
>
> 0
>> (si::system "./main.exe")
>
> HOLA
> 0

Here is my attempt at an exact mimic of your example, after fixing the
problems in my compiler invocation I am still seeing the same error, my
session is below.  I'm not clear on;

- 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?

Thanks so much for your help -- Eric

$ 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"/usr/local/lib/ecl-11.1.1/cmp.fas"
("CMP")
> (c::build-shared-library "foo" :lisp-files (list (compile-file "foo" :system-p t)))

;;;
;;; Compiling foo.
;;; OPTIMIZE levels: Safety=2, Space=0, Speed=3, Debug=0
;;;
;;; Compiling (DEFUN FOO ...).
;;; End of Pass 1.
;;; Emitting code for FOO.
;;; Note:
;;;   Invoking external command:
;;;   gcc -I. -I/usr/local/include/ -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -g -O2 -fPIC -Dlinux -O2 -w -c foo.c -o foo.o 
;;; Finished compiling foo.
;;;
;;; Note:
;;;   Library initialization function is main_dll_FOO
;;; Note:
;;;   Invoking external command:
;;;   gcc -I. -I/usr/local/include/ -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -g -O2 -fPIC -Dlinux -O2 -w -c /tmp/eclinityidRJN.c -o /tmp/eclinityidRJN.o 
;;; Note:
;;;   Invoking external command:
;;;   gcc -o libfoo.so -L/usr/local/lib/ /tmp/eclinityidRJN.o foo.o -Wl,--rpath,/usr/local/lib/ -shared -lecl -lgmp -lffi -ldl -lm 
#P"libfoo.so"
> (si::system "gcc -o use use.c -L/usr/local/lib -L./ -lfoo -lecl")
/tmp/cc2JAuv0.o: In function `main':
use.c:(.text+0x47): undefined reference to `foo'
collect2: ld returned 1 exit status

1
> 

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




More information about the ecl-devel mailing list