I am almost sure that I did this once a year ago... Not it just wouldn't work. Weird. I must be making a minor mistake somewhere... Please help!
I have the following toy c code:
// testdll.c
int sum(int a, int b)
{
return(a+b);
}
And then, since I am using Windows 7, I used WinSDK 7.1's x64 C/C++ compiler to compile it:
cl testdll.c /TC /LD
The output is testdll.dll.
Then, in my Python 3.3, I used:
In [12]: import ctypes
In [13]: lib = ctypes.cdll.LoadLibrary('./testdll.dll')
In [14]: lib
Out[14]: <CDLL './testdll.dll', handle f7000000 at a43ea58>
In [15]: lib.sum
Traceback (most recent call last):
File "<ipython-input-15-309017dbbec8>", line 1, in <module>
lib.sum
File "C:\WinPython2\python-2.7.6.amd64\lib\ctypes\__init__.py", line 378, in __getattr__
func = self.__getitem__(name)
File "C:\WinPython2\python-2.7.6.amd64\lib\ctypes\__init__.py", line 383, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'sum' not found
It can't find this function! It's driving me crazy. Since I used pure C, and I used /TC during compiling, it shouldn't be a name mangling issue.
Any idea would be appreciated. Thank you all so much!
EDIT 2014/02/13 I tried to compile it with gcc also, but with no luck. Same old problem happens. I used dir() in python, and realized everything should be in it - just not the correct name, by which I mean it can't be called via fun.sum. It is able to recognize that the result type of the function is an int.
In [34]: dir(lib)
Out[34]:
['_FuncPtr',
'__class__',
'__delattr__',
'__dict__',
'__doc__',
'__format__',
'__getattr__',
'__getattribute__',
'__getitem__',
'__hash__',
'__init__',
'__module__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'_func_flags_',
'_func_restype_',
'_handle',
'_name']
In [35]: lib._func_restype_
Out[35]: ctypes.c_long