Re: [cython-users] C API Declaration Seg Fault

60 views
Skip to first unread message

Stefan Behnel

unread,
Jun 28, 2012, 1:42:37 AM6/28/12
to cython...@googlegroups.com
Daniel Liu, 28.06.2012 01:52:
> I've been trying to run the delorean.pyx and marty.c example for C API
> Declaration (http://docs.cython.org/src/userguide/external_C_code.html) and
> I've been getting segmentation faults:
>
> delorean.pyx:
> # delorean.pyx
>
> ctypedef public struct Vehicle:
> int speed
> float power
>
> cdef public api void activate(Vehicle *v):
> if v.speed >= 88 and v.power >= 1.21:
> print "Time travel achieved"
>
> marty.c:
> #include "delorean_api.h"
>
> Vehicle car;
>
> int main(int argc, char *argv[]) {
> import_delorean();
> car.speed = atoi(argv[1]);
> car.power = atof(argv[2]);
> activate(&car);
> }
>
> compiling:
> cython delorean.pyx
> gcc delorean.c marty.c -I/usr/include/python2.7/ -lpython2.7
>
> running:
> ./a.out 100 2
> Segmentation fault (core dumped)
>
> Does anyone know what might be happening?

You didn't initialise the Python runtime.

http://docs.python.org/c-api/init.html

Take a look at the embedding example in Demos/embed/

Stefan

Stefan Behnel

unread,
Jun 28, 2012, 1:54:42 AM6/28/12
to cython...@googlegroups.com
Stefan Behnel, 28.06.2012 07:42:
> Take a look at the embedding example in Demos/embed/

Oh, and we should finally add a doc chapter on that to the tutorials, also
explaining a bit of what goes on here and presenting the two ways
(main/non-main) of using --embed.

Would anyone have the time to start writing one up?

Stefan
Reply all
Reply to author
Forward
0 new messages