segfault when calling a cython module from C++

961 views
Skip to first unread message

Alex Van Houten

unread,
Nov 16, 2011, 6:03:19 AM11/16/11
to cython...@googlegroups.com
I am trying to build a minimal example to show how one can call
a Cython (0.15.1) module from C++, but it segfaults.

This is some_cython.pyx:
"
cdef void some_module():

print "These words are written by calling a Cython module."
"
cython --cplus some_cython.pyx---> some_cython.cpp

g++ -c some_cython.cpp -I/usr/include/python2.7 -o some_cython.obj
--->some_cython.obj

Now the C++ code, simple.cpp
"
#include "Python.h"

#include "some_cython.h"

int main()

{

void some_module();

some_module();

return 1;

}
"
compile
g++ -c simple.cpp -I/usr/include/python2.7 -o simple.obj
---> simple.obj

link with some_cython.obj
g++ -shared simple.obj some_cython.obj -o simple
---> simple

execute
("." is in my PATH)
simple--->Segmentation fault (core dumped)

Any clue?

Thnx,
Alex.


Stefan Behnel

unread,
Nov 16, 2011, 7:11:21 AM11/16/11
to cython...@googlegroups.com
Alex Van Houten, 16.11.2011 12:03:

Well, this obviously applies:

http://docs.python.org/extending/embedding.html

Stefan

Alex Van Houten

unread,
Nov 16, 2011, 11:14:22 AM11/16/11
to cython...@googlegroups.com
Stefan Behnel <stefan_ml <at> behnel.de> writes:

> Well, this obviously applies:
>
> http://docs.python.org/extending/embedding.html
>

Obviously the api keyword was missing.
I just ran into the proper docs:
http://docs.cython.org/src/userguide/external_C_code.html

However, I am still running into segfaults if I try to
run the example (delorean.pyx, marty.c) from that
document. Are there some typos perhaps?

Can you get marty
# 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);
}

to call delorean

# delorean.pyx
cdef public struct Vehicle:
int speed
float power

cdef api void activate(Vehicle *v):
if v.speed >= 88 and v.power >= 1.21:
print "Time travel achieved"

???


mark florisson

unread,
Nov 16, 2011, 11:33:39 AM11/16/11
to cython...@googlegroups.com

Did you read Stefan's link? You cannot just use Python and import
modules without initializing Python first.

Alex Van Houten

unread,
Nov 17, 2011, 11:33:48 AM11/17/11
to cython...@googlegroups.com
mark florisson <markflorisson88 <at> gmail.com> writes:

> Did you read Stefan's link? You cannot just use Python and import
> modules without initializing Python first.
>
Yep, thanks, that helped.
Py_Initialize(); and Py_Finalize(); obviously had to be added.

Still, I had to replace
cdef public struct Vehicle:
by
ctypedef public struct Vehicle:
or I would get
"error: unknown type name ‘Vehicle’"

Moreover, I still get a segfault from
activate(&car);

Any clue?

Thanks,
Alex.


Alex Van Houten

unread,
Nov 23, 2011, 9:24:18 AM11/23/11
to cython...@googlegroups.com
Alex Van Houten <sparrow2867 <at> yahoo.com> writes:

> Moreover, I still get a segfault from
> activate(&car);
>
> Any clue?

Manually adjusting delorean_api.h by adding these two lines
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append(\".\")");

below
static int import_delorean(void) {
PyObject *module = 0;

fixes the problem. Inspired by this
http://boost.2283326.n4.nabble.com/
failed-in-PyImport-Import-td2700595.html
link.

Alex.


Alex Van Houten

unread,
Nov 23, 2011, 9:43:07 AM11/23/11
to cython...@googlegroups.com
Alex Van Houten <sparrow2867 <at> yahoo.com> writes:

> Manually adjusting delorean_api.h by adding these two lines
> PyRun_SimpleString("import sys");
> PyRun_SimpleString("sys.path.append(\".\")");
>
> below
> static int import_delorean(void) {
> PyObject *module = 0;
>
> fixes the problem.

Much cleaner solution is of course to add those two lines to
marty.c:
#include "delorean_api.h"

Vehicle car;

int main(int argc, char *argv[]) {

Py_Initialize();


PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append(\".\")");

import_delorean();
......

Alex.


Reply all
Reply to author
Forward
0 new messages