[Ecls-list] generated shared library does not contain the defined function

Ala'a Mohammad amalawi at gmail.com
Mon May 13 10:33:00 UTC 2013


Hi,

This is my first try at ECL, and need help in figuring out what is missing.
I had tried googling for similar examples but did not found one that demoed
what i wanted.

I'm trying to define a function named sum-array in CL, which is defined in
a file called foo.lisp

I compiled foo.lisp, generated foo.o, and then used that to create a shared
library libtest.so

Finally I created a C file named try.c that will load libtest.so
dynamically, which succeeded, but failed to locate the symbol L1sum_array
or demo_sum_array.

I double checked the files and the online resources (docs, and some online
tutorials), but couldn't find a clue on what I was missing.

the files and the steps used to compile the example are attached below for
reference.

Ala'a

----------------------------------------------------------------------

;; foo.lisp
(defpackage "DEMO"
  (:use :cl)
  (:export "SUM-ARRAY"))

;; using the following
;;;(declaim (si::c-export-fname sum-arary))
;; gave me
;;Unknown declaration specifier SI::C-EXPORT-FNAME
;; so instead I tried 'proclaim', but it seem to have
;; no effect as the generated symbol name is L1sum_array rather than
demo_sum_array
(proclaim '(si::c-export-fname sum-arary))

(defun sum-array (array)
  (loop for i from 0 below (length array)
        summing (aref array i)))

----------------------------------------------------------------------


; launch ECL in a console
; ecl -norc
; then execute the following.
;> (compile-file "foo.lisp" :c-file t :h-file t :data-file t :system-p t)
;> (c:build-shared-library "test" :lisp-files '("foo.o"))

----------------------------------------------------------------------

// try.c
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>

int main(char **argv, int argc) {
  void * libhandle;
  int (*demo_sum_array)(int[]);
  int nums[10] = {1, 2, 0, 0, 4, 5, 6, 9, 9, 17};

  // Load Shared library
  libhandle = dlopen("./libtest.so",RTLD_NOW);
  if(libhandle==NULL) {
    fprintf(stderr, "Couldn't open library: %s\n",
    dlerror());
    exit(1);
  }

  // summ-array
  ////demo_sum_array  = dlsym(libhandle,"demo_sum_array");
  demo_sum_array  = dlsym(libhandle,"L1sum_array");
  if(demo_sum_array == NULL) {
    /* ERROR HANDLING */
    fprintf(stderr, "%s\n", dlerror());
    exit(1);
  }

  printf("Return is %i\n",(*demo_sum_array)(nums));

  dlclose(libhandle);

  return 0;

}

----------------------------------------------------------------------

: gcc -g try.c -o try -L./ -ltest -ldl

: ./try
./libtest.so: undefined symbol: L1sum_array
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/ecl-devel/attachments/20130513/f275a8f6/attachment.html>


More information about the ecl-devel mailing list