ich eine einfache Testfunktion auf C++ haben:ctypes Fehler Attribute Symbol nicht gefunden, OS X 10.7.5
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <locale.h>
#include <wchar.h>
char fun() {
printf("%i", 12);
return 'y';
}
Compilierung:
gcc -o test.so -shared -fPIC test.cpp
und es in Python mit ctypes mit:
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())
aber dann bekomme ich einen Fehler:
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
Wo ist ein Problem?
mit:
Mac Os X 10.7.5 and Python 2.7
Does mit 'gcc' verknüpfen, um ein C++ Programm auf den richtigen C++ Runtime in OS X? Ich würde erwarten, dass es 'g ++' benötigt. Ich weiß, dass es in diesem Fall keine Rolle spielt, da der Beispielcode nur C-Standardbibliotheksfunktionen verwendet. – eryksun
@eryksun: 'gcc' kompiliert es als C++, wenn die Quelldatei das Suffix' .cpp' hat, aber Sie haben Recht, 'g ++' wird bevorzugt. Ich habe es in meiner Antwort nicht erwähnt, weil es einen Kommentar dazu gab (deins?). Es ist nachweisbar C++, weil der Name mangling anders ist als das bedingte 'externe 'C'' (siehe Text des Posts). – cdarke
'gcc' kompiliert die Quelle als C++, aber unter Linux wird keine Verbindung mit libstdC++ .so.6 hergestellt, daher schlägt das Laden der Bibliothek fehl, wenn die C++ - Bibliothek benötigt wird. – eryksun