I have a simple test function on C++:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <locale.h>
#include <wchar.h>
char fun() {
printf( "%i", 12 );
return 'y';
}
compiling:
gcc -o test.so -shared -fPIC test.cpp
and using it in python with ctypes:
from ctypes import cdll
from ctypes import c_char_p
lib = cdll.LoadLibrary('test.so')
hello = lib.fun
hello.restype = c_char_p
print('res', hello())
but then I get an error:
Traceback (most recent call last): File "./sort_c.py", line 10, in <module>
hello = lib.fun File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 366, in __getattr__
func = self.__getitem__(name) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 371, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: dlsym(0x100979b40, fun): symbol not found
Where is a problem?
using:
Mac Os X 10.7.5 and Python 2.7
Your first problem is C++ name mangling. If you run
nm
on your.so
file you will get something like this:If you mark it as C style when compiled with C++:
nm
gives:Your second problem is that the python will die with a
Segmentation fault: 11
(on OS X). The C++ is returning achar
, whereas you are marking it in python as a pointer to a char. Use:instead (alter your
import
statement to match).EDIT: as @eryksun pointed out, you should not use
gcc
, you should useg++
instead. Otherwise the correct C++ runtime will not be linked. To check onOS X
:(
ldd
, the tool normally used on UNIX/Linux, is not distributed with OS X)