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.
Well, this obviously applies:
http://docs.python.org/extending/embedding.html
Stefan
> 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"
???
Did you read Stefan's link? You cannot just use Python and import
modules without initializing Python first.
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.
> 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.
> 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.