Problem when import C model

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • baileyxia@gmail.com

    #1

    Problem when import C model

    Hi, all
    I am learning how to import c code in python.
    Here is my simple code foo.c:
    =============== ======
    #include <Python.h>
    void bar()
    {
    printf("Hello! C wrap!");
    }
    static PyObject *foo_bar(PyObje ct *self, PyObject *args) {
    /* Do something interesting here. */
    bar();
    Py_RETURN_NONE;
    }
    static PyMethodDef foo_methods[] = {
    { "bar", (PyCFunction)fo o_bar, METH_NOARGS, NULL },
    { NULL, NULL, 0, NULL }
    };
    PyMODINIT_FUNC initfoo() {
    Py_InitModule3( "foo", foo_methods, "My first extension module.");
    }
    =============== ======
    I use gcc to compile the foo.c:
    gcc -shared -I/usr/local/python/2.4.2/include/python2.4 -fPIC foo.c -o
    foo.so

    Problem is:
    I can import foo on linux 64-bit platform if I also run gcc on linux
    64-bit platform.
    But I can not import foo on linux 64-bit platform if I run gcc on linux
    32-bit platform.
    Here is the error messege:
    =============== ======
    traceback (most recent call last):
    File "<stdin>", line 1, in ?
    ImportError: ./foo.so: cannot open shared object file: No such file or
    directory
    =============== ======

    I can not figure out what cause this problem. wrong gcc option? wrong
    python code?

  • cmdrrickhunter@yaho.com

    #2
    Re: Problem when import C model

    this is more of a linux question than a python question, so you may get
    better luck with asking there.

    What you describe makes perfect sense to me. If python is a 64 bit
    program, it can only link to 64 bit libraries. If you compiled your
    library in 32-bit mode, then the library headers will indicate this,
    and linux's library loading code will ignore it when loading libraries
    for 64-bit programs.

    I think there' might be a trick that lets you compile ELF files with
    both 64-bit and 32-bit code, but actually doing so is outside of my
    expertise.
    baileyxia@gmail .com wrote:
    Hi, all
    I am learning how to import c code in python.
    Here is my simple code foo.c:
    =============== ======
    #include <Python.h>
    void bar()
    {
    printf("Hello! C wrap!");
    }
    static PyObject *foo_bar(PyObje ct *self, PyObject *args) {
    /* Do something interesting here. */
    bar();
    Py_RETURN_NONE;
    }
    static PyMethodDef foo_methods[] = {
    { "bar", (PyCFunction)fo o_bar, METH_NOARGS, NULL },
    { NULL, NULL, 0, NULL }
    };
    PyMODINIT_FUNC initfoo() {
    Py_InitModule3( "foo", foo_methods, "My first extension module.");
    }
    =============== ======
    I use gcc to compile the foo.c:
    gcc -shared -I/usr/local/python/2.4.2/include/python2.4 -fPIC foo.c -o
    foo.so
    >
    Problem is:
    I can import foo on linux 64-bit platform if I also run gcc on linux
    64-bit platform.
    But I can not import foo on linux 64-bit platform if I run gcc on linux
    32-bit platform.
    Here is the error messege:
    =============== ======
    traceback (most recent call last):
    File "<stdin>", line 1, in ?
    ImportError: ./foo.so: cannot open shared object file: No such file or
    directory
    =============== ======
    >
    I can not figure out what cause this problem. wrong gcc option? wrong
    python code?

    Comment

    • Nick Craig-Wood

      #3
      Re: Problem when import C model

      cmdrrickhunter@ yaho.com <conrad.ammon@g mail.comwrote:
      What you describe makes perfect sense to me. If python is a 64 bit
      program, it can only link to 64 bit libraries. If you compiled your
      library in 32-bit mode, then the library headers will indicate this,
      and linux's library loading code will ignore it when loading libraries
      for 64-bit programs.
      32 bit and 64 bit are completely different architectures - you can't
      mix and match them in one executable.

      The python solution is to use distutils and build a setup.py then you
      can compile for each platform easily.
      I think there' might be a trick that lets you compile ELF files
      with both 64-bit and 32-bit code, but actually doing so is outside
      of my expertise.
      I don't think so.

      You can choose which you compile with. On 32 bit you'll compile 32
      bit by default. If you want 64 bit choose -m64, eg

      echo <<'#END' >z.c
      #include <stdio.h>
      int main(void)
      {
      printf("Hello\n ");
      return 0;
      }
      #END
      gcc -c -m32 -o z32.o z.c
      gcc -c -m64 -o z64.o z.c
      file z*.o

      gives

      z32.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
      z64.o: ELF 64-bit LSB relocatable, AMD x86-64, version 1 (SYSV), not stripped

      --
      Nick Craig-Wood <nick@craig-wood.com-- http://www.craig-wood.com/nick

      Comment

      Working...