Advertisement
sylvaticus

fooWrapper.py

May 22nd, 2014
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # Example to wrap C functions in python using ctypes
  4.  
  5. # See http://stackoverflow.com/questions/145270/calling-c-c-from-python
  6.  
  7. # ** Python wrapper file **
  8. # To be used with example C++ file: http://pastebin.com/pyUXf3RW
  9. # Tested in Ubuntu 14.04
  10.  
  11. from ctypes import cdll
  12. from ctypes import c_char_p
  13. from ctypes import c_double
  14. lib = cdll.LoadLibrary('./libfoo.so')
  15.  
  16. class Foo(object):
  17.     def __init__(self):
  18.         self.obj = lib.Foo_new()
  19.  
  20.     def bar(self):
  21.         lib.Foo_bar(self.obj)
  22.    
  23.     def doubleme(self, intnumber):
  24.         return lib.Foo_doubleme(self.obj, intnumber)
  25.      
  26.     def printfloat(self, doublenumber):
  27.         lib.Foo_printfloat(self.obj, c_double(doublenumber))
  28.        
  29.  
  30. f = Foo()
  31. f.bar()
  32. test = f.doubleme(4)
  33. f.printfloat(6.3)
  34.  
  35. print test
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement