Error "wrong ELF class" using ctypes

11,719

You need to compile the object file as 64 bit and position independent, then link the object file to the shared library with 64 bit options. Something like:

gcc -c -fPIC -m64 -std=c99 -lm -D_GNU_SOURCE -Wall -pedantic -fopenmp -o foo.o foo.c
gcc -m64 -shared -Wl,-soname,libfoo.so -o libfoo.so foo.o

That should get you a 64 bit library on any gnu toolchain I have used. If you are still getting errors, something might be broken in your toolchain or Python.

Share:
11,719

Related videos on Youtube

Framester
Author by

Framester

Updated on June 04, 2022

Comments

  • Framester
    Framester almost 2 years

    after changing from a 32 to 64Bit Ubuntu installation my python+ctypes+c99 code is broken. I read so far, that the error ./libfoo.so: wrong ELF class: ELFCLASS32 means, that my libfoo.so[1] is a 32Bit library and that python wants a 64Bit version. How do I tell gcc/ctypes to generate the library as 32Bit?

    Thanks for any feedback!

    Error message:

     File "foo.py", line 8, in <module>
        autofoo=cdll.LoadLibrary("./libfoo.so")
      File "/usr/lib/python2.6/ctypes/__init__.py", line 431, in LoadLibrary
        return self._dlltype(name)
      File "/usr/lib/python2.6/ctypes/__init__.py", line 353, in __init__
        self._handle = _dlopen(self._name, mode)
    OSError: ./libfoo.so: wrong ELF class: ELFCLASS32
    

    [1] I compile libfoo.so with gcc -c -std=c99 -lm -D_GNU_SOURCE -Wall -pedantic -fopenmp -ofoo.o foo.c

    • lunaryorn
      lunaryorn almost 13 years
      Can't you just use a 64-bit library?
    • Framester
      Framester almost 13 years
      Hi lunaryorn. I wrote the library 'libfoo.so' myself, so I assume, I have to set a flag or something to tell gcc to create a 64Bit version.
  • Framester
    Framester almost 13 years
    Hi IVA, thanks for the idea! I tried that, but it did not change the error. :/
  • Framester
    Framester almost 13 years
    The second line did it for me. Thanks!
  • Garrett Hyde
    Garrett Hyde over 11 years
    Your link is currently broken.