Sorry, Gmail unfortunately didn't remind me to reply to all, so here is a separate forward to the list of the reply I sent to Javier.
// Jarkko
---------- Forwarded message ---------- From: Jarkko Kniivilä <jkniiv(a)gmail.com> Date: Fri, Mar 29, 2013 at 12:42 PM Subject: Re: [Ciao-users] Platform-dependent library generation with ciaoc To: javier.g.rueda(a)gmail.com
Hi,
I haven't had the chance to test ciaoc out yet but if you are at least able to build .a libraries with it, you can use the newer cffihttps://pypi.python.org/pypi/cffi instead of ctypes in Python to access them. Here's how. Let's say you have defined your own quicksort routine in C called string_vector_quicksort() with the following prototype:
void string_vector_quicksort(int c, char *v[]);
i.e. a routine you could call with main()'s argc and argv for instance to sort the command line arguments or similar string "vectors". Now you do the following in Python to access the routine:
''' #!/usr/bin/env python
proj_dir="/path/to/where/you/keep/the/c/library/project"
from cffi import FFI ffi = FFI() ffi.cdef(""" void string_vector_quicksort(int c, char *v[]); """)
C = ffi.verify(""" #include "myquicksort.h" """, library_dirs=[proj_dir], libraries=["myquicksort"], include_dirs=[proj_dir])
# Let's construct the vector to sort. # Equivalent to the following C initializer: { "foo", "baar", "bazz", "quux" } vector_dont_garbage_collect = [ffi.new("char[]", "foo"), ffi.new("char[]", "baar"), ffi.new("char[]", "bazz"), ffi.new("char[]", "quux")] vector = ffi.new("char *[]", vector_dont_garbage_collect)
# Let's finally call our routine C.string_vector_quicksort(len(vector), vector)
# ... and print out the sorted results. for x in vector: print ffi.string(x) '''
The above actually calls your C compiler to make a temporary extension for Python to load. It assumes you have the files "myquicksort.h" and "libmyquicksort.a" in the project directory (proj_dir above).
I hope that gave you some pointers. Please read the cffi docs and especially the part about how C code "verification" aka just-in-time compilation works: http://cffi.readthedocs.org/en/release-0.5/#the-verification-step.
// Jarkko
On Thu, Mar 28, 2013 at 9:01 PM, Javier G. Rueda <javier.g.rueda(a)gmail.com>wrote:
Hi,
¿Is it possible to generate a library (i.e .so in linux) with ciaoc?
I would like to call ciao predicates from python using ctypes, and a .so library is needed.
Thank you,
Regards Javier
Ciao-users mailing list Ciao-users(a)clip.dia.fi.upm.es http://clip.dia.fi.upm.es/cgi-bin/mailman/listinfo/ciao-users