<div dir="ltr"><div>I mistakenly assumed that 'defined protocol' was for when linking with libecl. and thought that the c:build-shared-library was similar to LispWorks delivered shared libraries (see the attached example that I was trying to port from LW).<br>
</div><div><br></div><div style>So now I need to execute that defined protocol, and then:</div><div style>- load my library (my CL library)</div><div style><div>- define a result object with <span style="line-height:19px;text-align:justify">cl_object.</span></div>
<div>- create a CL array object via 'si_make_array'<br></div></div><div style>- fill the created array with the data.<br></div><div style>- call the the function sum_array and store the result in the result object.</div>
<div style><br></div><div style>Thanks to all for the help,</div><div style><br></div><div style>Ala'a</div><div style><br></div><div style><br></div><div style>LW shared library example:</div><div style><br></div><div style>
---- example.lisp</div><div style><div>(in-package "CL-USER")</div><div><br></div><div>(fli:define-foreign-callable ("say_hello" :result-type :void)</div><div> ()</div><div> (write-line "Hello From Shared Library.")</div>
<div> (force-output)</div><div> (values))</div><div><br></div><div>(fli:define-foreign-callable ("square" :result-type :int)</div><div> ((num :int))</div><div> (* num num))</div><div><br></div><div>(fli:define-foreign-callable ("sum_array" :result-type :int)</div>
<div> ((nums (:c-array :int 10))</div><div> (size :int))</div><div> (loop for i from 0 below size</div><div> summing (fli:foreign-aref nums i)))</div><div><br></div><div>(fli:define-foreign-callable ("sum_dyn_array" :result-type :int)</div>
<div> ((dyn_nums (:pointer :int))</div><div> (size :int))</div><div> (loop repeat size</div><div> summing (fli:dereference dyn_nums :type :int)</div><div> do (fli:incf-pointer dyn_nums))) </div><div>
<br></div><div><br></div><div><br></div><div style>------ deliver.lisp ------</div><div style><br></div><div style><div>(in-package "CL-USER")</div><div><br></div><div>(load-all-patches)</div><div><br></div><div>
(compile-file (current-pathname "example.lisp") :load t)</div><div><br></div><div>(deliver nil</div><div> (current-pathname "lw-shared" nil)</div><div> 5</div><div> :dll-exports '("square" "say_hello" "sum_array" "sum_dyn_array"))</div>
<div><br></div><div><br></div><div>------ command line - generate shared library --------<br></div><div style><br></div><div style>lispworks-6-1-0-x86-linux -build deliver.lisp<br></div><div style><br></div><div style><br>
</div><div style><br></div><div style>------ try.c ------</div><div style><br></div><div style><div>#include <stdio.h></div><div>#include <stdlib.h></div><div>#include <dlfcn.h></div><div><br></div><div>
void (*say_hello)(void);</div><div>int (*square)(int);</div><div>int (*sum_array)(int *array, int size);</div><div>int (*sum_dyn_array)(int *array, int size);</div><div><br></div><div>int main(int argc, char **argv)</div>
<div>{</div><div> void *handle;</div><div> int index;</div><div> int total_sum = 0;</div><div> int number = 10;</div><div> int nums[10] = {17, 2, 0, 0, 4, 5, 6, 9, 9, 15};</div><div> int *dyn_nums;</div><div> int dyn_array_size = 20;</div>
<div><br></div><div> handle = dlopen("/tmp/lw-shared.so", RTLD_LAZY);</div><div> if (!handle) {</div><div> fprintf(stderr, "Failed to load shared library:\n %s\n", dlerror());</div><div> exit(1);</div>
<div> }</div><div><br></div><div> say_hello = dlsym(handle, "say_hello");</div><div> if (!say_hello) {</div><div> fprintf(stderr, "Failed to find symbol:\n %s\n",</div><div> dlerror());</div>
<div> exit(1);</div><div> }</div><div> (*say_hello)();</div><div><br></div><div> square = dlsym(handle, "square");</div><div> if (!square) {</div><div> fprintf(stderr, "Failed to find symbol:\n %s\n",</div>
<div> dlerror());</div><div> exit(1);</div><div> }</div><div> printf("Square of %i is %i\n", number, (*square)(number));</div><div><br></div><div> sum_array = dlsym(handle, "sum_array");</div>
<div> if (!sum_array) {</div><div> fprintf(stderr, "Failed to find symbol:\n %s\n",</div><div> dlerror());</div><div> exit(1);</div><div> }</div><div> printf("sum of nums array from LW is %i\n",(*sum_array)(nums, 10));</div>
<div> </div><div> for(index=0; index<10; ++index){</div><div> total_sum += nums[index];</div><div> }</div><div> printf("sum of nums array from C is %i\n", total_sum);</div><div><br></div><div><br></div>
<div> sum_dyn_array = dlsym(handle, "sum_dyn_array");</div><div> if (!sum_dyn_array) {</div><div> fprintf(stderr, "Failed to find symbol:\n %s\n",</div><div> dlerror());</div><div> exit(1);</div>
<div> }</div><div><br></div><div> dyn_nums = malloc(dyn_array_size * sizeof(int));</div><div> if (!dyn_nums) {</div><div> printf("Error allocating memory!\n");</div><div> return 1;</div><div> }</div><div>
<br></div><div> for(index=0; index<dyn_array_size; ++index){</div><div> dyn_nums[index] = index;</div><div> }</div><div><br></div><div> printf("sum of dyn_nums array from LW is %i\n",(*sum_dyn_array)(dyn_nums, dyn_array_size));</div>
<div><br></div><div> total_sum = 0;</div><div> for(index=0; index<dyn_array_size; ++index){</div><div> total_sum += dyn_nums[index];</div><div> }</div><div> printf("sum of dyn_nums array from C is %i\n", total_sum);</div>
<div><br></div><div> free(dyn_nums);</div><div><br></div><div> return 0;</div><div>}</div><div><br></div></div></div></div><div><div>------ command line - compile try.c --------<br></div></div><div>gcc try.c -o try -ldl -lpthread<br>
</div><div><div><br></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, May 20, 2013 at 1:08 AM, Juan Jose Garcia-Ripoll <span dir="ltr"><<a href="mailto:juanjose.garciaripoll@gmail.com" target="_blank">juanjose.garciaripoll@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 dir="ltr"><div class="im">On Mon, May 13, 2013 at 12:33 PM, Ala'a Mohammad <span dir="ltr"><<a href="mailto:amalawi@gmail.com" target="_blank">amalawi@gmail.com</a>></span> wrote:<br>
</div><div class="gmail_extra"><div class="gmail_quote"><div class="im">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div>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.</div>
</div></blockquote><div><br></div></div><div>There are many examples out there how to call Common Lisp functions. Indeed many in this very same mailing list explain how to call cl_boot(), ecl_read_from_cstring(), cl_funcall() and friends. Directly calling a Common Lisp function compiled by you by name is not recommended.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div><div>(proclaim '(si::c-export-fname sum-arary))<br>
</div></div></div></blockquote><div><br></div><div>In order to have compile-time side effects you need to fix the name and use</div><div>(eval-when (:compile-toplevel)</div><div> (proclaim '(si::c-export-fname sum-array))) </div>
<div class="im">
<div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div>
</div><div> // summ-array<br></div><div><div> ////demo_sum_array = dlsym(libhandle,"demo_sum_array");</div>
<div> demo_sum_array = dlsym(libhandle,"L1sum_array");</div><div> if(demo_sum_array == NULL) {</div><div> /* ERROR HANDLING */</div><div> fprintf(stderr, "%s\n", dlerror());</div><div> exit(1);</div>
<div> }</div></div></div></blockquote></div></div><div><br></div><div>Broken, broken, broken. ECL has to be bootstrapped before calling any function. This involves a well defined protocol that starts by calling cl_boot() with two arguments and, optionally, registering any additional threads from which code may be invoked. Then you can start using Common Lisp functions and objects.</div>
<div><br></div><div>Juanjo</div><span class="HOEnZb"><font color="#888888"><div><br></div>-- <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>
</font></span></div></div>
</blockquote></div><br></div>