1

I have a small cython module called deLorean.pyx

cdef public struct Vehicle:
    int speed
    float power

cdef public api void activate(int v):
        print "Time travel achieved at " + str(v) + " mph."

I also have a setup.py file that looks like this:

from distutils.core import setup
from Cython.Build import cythonize

setup(name = 'First try', ext_modules = cythonize(["deLorean.pyx"]),)

When I go to compile the cython code using this: cython deLorean.pyx

This will then generate *.h, *.c, and *_api.h files.

I also have a c program called marty.c that looks like this:

#include "Python.h"
#include "deLorean_api.h"

#include <stdlib.h>

struct Vehicle car;

int main(int argc, char** agrv){
    printf("HELLO");
    Py_Initialize();
    import_deLorean();
    car.speed = 33;
    car.power = 12.3;
    printf("speed: %d, power: %f", car.speed, car.power);
    activate(12);
    Py_Finalize();
    return 0;
}

I then compile the entire module using this:

gcc -fPIC -L/usr/lib -I/usr/local/include/python2.7 -lpython2.7 deLorean.c marty.c -o deLorean -g

This compiles with these notes:

/usr/bin/ld: skipping incompatible /usr/lib/libc.so when searching for -lc

/usr/bin/ld: skipping incompatible /usr/lib/libc.a when searching for -lc

This creates an a.out file; however, it seg faults when run.

When I run it in gdb, this is the output:

(gdb) r
Starting program: /root/Asta/Cython-0.22.1/deLorean
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb)

I have played around with marty.c and I have narrowed down the culprit to when the activate() function is called. Is there something I have overlooked? What could possible be causing this behavior?

  • Is this just a special case of http://stackoverflow.com/questions/16927885/usr-bin-ld-skipping-incompatible-foo-so-when-searching-for-foo?rq=1? I suspect the seg fault may be unrelated, but I don't know... – DavidW Jul 08 '15 at 06:31
  • More importantly, my impression is that when you use the `import_module` mechanism in C, you shouldn't link your C program with the module, but instead should build the Cython module as an .so file. Calling `import_module` loads the .so file (giving you access to the functions), so it's possible you end up with a conflict by both linking and loading the .so file. – DavidW Jul 08 '15 at 09:02
  • @DavidW When I create a .so library, I use these commands: `gcc -lpython2.7 -L/usr/lib -I/usr/local/include/python2.7 -m32 -shared -o libdeLorean.so -fPIC deLorean.c` **In file included from /usr/local/include/python2.7/Python.h:58, from deLorean.c:4: /usr/local/include/python2.7/pyport.h:886:2: error: #error "LONG_BIT defi appears wrong for platform (bad gcc/glibc config?)."** But I still get errors – Asta Saidak Jul 08 '15 at 17:58
  • Trying to debug building the cython extension is likely to be beyond me I'm afraid (I suspect all I'd be able to say is "it works for me"). Have you looked at the setuptools based approach given in the Cython documentation? It does atleast ensure it gets exactly the right arguments to gcc. – DavidW Jul 08 '15 at 21:09
  • Ok, so for anyone wondering how I solved my problem, here's how. I simply made sure that I had a 64 bit python version installed somewhere, and then once I knew where I would just add those locations to the compilation. I also had to change the LD_LIBRARY_PATH to that same location. Best of Luck! – Asta Saidak Jul 08 '15 at 22:05

0 Answers0