Error "wrong ELF class" using ctypes
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.
Related videos on Youtube
Framester
Updated on June 04, 2022Comments
-
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 mylibfoo.so
[1] is a 32Bit library and that python wants a 64Bit version. How do I tellgcc/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
withgcc -c -std=c99 -lm -D_GNU_SOURCE -Wall -pedantic -fopenmp -ofoo.o foo.c
-
lunaryorn almost 13 yearsCan't you just use a 64-bit library?
-
Framester almost 13 yearsHi 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 almost 13 yearsHi IVA, thanks for the idea! I tried that, but it did not change the error. :/
-
Framester almost 13 yearsThe second line did it for me. Thanks!
-
Garrett Hyde over 11 yearsYour link is currently broken.