Your first problem is C ++ name change. If you run nm in your .so file, you will get something like this:
nm test.so 0000000000000f40 T __Z3funv U _printf U dyld_stub_binder
If you mark it as a C style when compiling with C ++:
#ifdef __cplusplus extern "C" char fun() #else char fun(void) #endif { printf( "%i", 12 ); return 'y'; }
nm gives:
0000000000000f40 T _fun U _printf U dyld_stub_binder
The second problem is that python will die using Segmentation fault: 11 (on OS X). C ++ returns a char , while you mark it in python as a pointer to a char. Using:
hello.restype = c_char
instead (change your import statement to match).
EDIT: as @eryksun pointed out, you should not use gcc , you should use g++ . Otherwise, the correct C ++ runtime will not be bound. To test OS X :
otool -L test.so
( ldd , a tool commonly used on UNIX / Linux, is not distributed with OS X)
source share