wrapper DLL lib func call dpi.cc
#ifdef __cplusplus extern "C" { int DPIAdd(int a, int b) { return a+b; } } #endif
gcc command
gcc -c -fPIC dpi.cc -o dpi.o gcc -shared -o dpi.so dpi.o
use python ctypes to call dpi
import sys from ctypes import * class DPIWrapper: """ wrapper class for DPI example """ def __init__(self, name="dpi.so"): """ name : shared library path """ self._dl = None try: self._dl = cdll.LoadLibrary(name) except IOError: print "load %s fail" %(name) def __del__(self): self._dl = None def WrapperAdd(self, a, b): """ wrapper add func from dpi.so """ return self._dl.DPIAdd(a,b) def main(): dpi = DPIWrapper(name="dpi.so") print "%d" %(dpi.WrapperAdd(1,2)) del dpi if __name__ == "__main__": main()Ref: http://www.adp-gmbh.ch/cpp/gcc/create_lib.html http://blog.ez2learn.com/2009/03/21/python-evolution-ctypes/ http://stackoverflow.com/questions/5081875/ctypes-beginner
沒有留言:
張貼留言