Hi William, hi Nathann,

On 29 Jun., 11:43, William Stein <wst...@gmail.com> wrote:
> > I have a C program without a makefile which is meant to be used from
> > the command line, and I would like to interface it with SAGE. I have
> > been told this should be done through libraries, and I do not have the
> > slightest idea of how it works in Cython. I just read those two
> > pages :
>
> >http://docs.sun.com/source/819-3690/Building.Libs.html
> >http://www.faqs.org/docs/Linux-HOWTO/Program-Library-HOWTO.html
>
> > They explain well enough how to build libraries with gcc, but I do not
> > know how to access their functions with Cython.
...
>
> Can you carefully try the examples and read the code here
>
> http://www.faqs.org/docs/Linux-HOWTO/Program-Library-HOWTO.html#MORE-...
>

I understood that Nathann knows how to build libraries with gcc, but
that he needs help on using them in Cython.

I think http://docs.cython.org/docs/external_C_code.html might help.

In a nutshell: You need to declare the C-types/functions/... in your
Cython program (this can be done in a .pxd-file, which is analogous to
a header file in C). The above page will tell you the differences in
syntax.

I try a brief example, hoping that I do not do too many mistakes.
Let bar.h be
  long foo(char*);
and let the function foo be defined somewhere, resulting in a static
library bar.a.

Your Cython code (say, wrapbar.pyx) must then do something like this:
cdef extern from "bar.h":
    long foo(char*)
def  test(s):
    if isinstance(s, basestring):
        return foo(s)
    raise TypeError

Running "sage -cython wrapbar.pyx" results in a C-file for your Cython
file. Then, you compile it, link against bar.a, and produce a shared
library wrapbar.so.

Then, in sage, you can do
  sage: from wrapbar import test
and use the function. If I am not mistaken, the conversion from a
python string to char* is automatically done.

Best regards,
   Simon
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to 
sage-devel-unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to