ctypes error AttributeError symbol not found, OS X 10.7.5

2024/11/18 16:22:36

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_plib = cdll.LoadLibrary('test.so')
hello = lib.fun
hello.restype = c_char_pprint('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

Answer

Your first problem is C++ name mangling. If you run nm on your .so file you will get something like this:

nm test.so
0000000000000f40 T __Z3funvU _printfU dyld_stub_binder

If you mark it as C style when compiled with C++:

#ifdef __cplusplus
extern "C" char fun()
#else
char fun(void)
#endif
{printf( "%i", 12 );return 'y';
}

nm gives:

0000000000000f40 T _funU _printfU dyld_stub_binder

Your second problem is that the python will die with a Segmentation fault: 11 (on OS X). The C++ is returning a char, whereas you are marking it in python as a pointer to a char. Use:

hello.restype = c_char

instead (alter your import statement to match).

EDIT: as @eryksun pointed out, you should not use gcc, you should use g++ instead. Otherwise the correct C++ runtime will not be linked. To check on OS X:

otool -L test.so

(ldd, the tool normally used on UNIX/Linux, is not distributed with OS X)

https://en.xdnf.cn/q/71041.html

Related Q&A

Filtering negative timedeltas

Consider a series holding timedelta64[ns] that measures at the time difference between two events A and B:> time_deltas499900 -1 days +23:45:13 499916 -1 days +23:50:57 499917 00:03:2…

What is the Matlab equivalent of the yield keyword in Python?

I need to generate multiple results but one at a time, as opposed to everything at once in an array.How do I do that in Matlab with a generator like syntax as in Python?

Convert from CMYK to RGB

Im having trouble converting a single page pdf (CMYK) to a jpg (RGB). When I use the code below, the colors in the jpg image are garish. Ive tried reading through the Wand docs, but havent found anythi…

TopologicalError: The operation GEOSIntersection_r could not be performed

Hi Guys, I am trying to map the district shapefile into assembly constituencies. I have shape files for [Both].Basically I have to map all the variables given at district level in census data to assemb…

Plot a function during debugging in Python

I used to work in Matlab and it is really convenient (when working with big arrays/matrices and nested functions) to visualize intermediate results during debugging using plot function. In Python I can…

getting last n items from queue

everything I see is about lists but this is about events = queue.queue() which is a queue with objects that I want to extract, but how would I go about getting the last N elements from that queue?

Paramiko ValueError p must be exactly 1024, 2048, or 3072 bits long

I am trying to connect SFTP using Python script. Im unable to connect due to "p error".import paramiko client = paramiko.SSHClient() client.load_system_host_keys() client.connect(####.com, us…

WindowsError: [Error 5] Access is denied using urllib2

Im getting a "WindowsError: [Error 5] Access is denied" message when reading a website with urllib2. from urllib2 import urlopen, Request from bs4 import BeautifulSouphdr = {User-Agent: Mozil…

Send key combination with python

I want to be able to send the key combination SHIFT + CTRL + . (dot) using the following code:import win32com.client as comclt wsh= comclt.Dispatch("WScript.Shell") wsh.SendKeys() So far I wa…

Taking data from drop-down menu using flask

Im completely new to flask, and really am completely lost with how to approach this. Ive looked into other SO questions but I cant seem to get this working regardless. I have a form as such: <form…