Advertisement
sylvaticus

foo.cpp

May 22nd, 2014
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. /* Example to wrap C functions in python using ctypes
  2.  
  3.    See http://stackoverflow.com/questions/145270/calling-c-c-from-python
  4.  
  5.    ** C++ example file **
  6.    compile it as a shared lib with
  7.    g++ -c -fPIC foo.cpp -o foo.o
  8.    and
  9.    g++ -shared -Wl,-soname,libfoo.so -o libfoo.so  foo.o
  10.  
  11.    Use it with Python wrapper at http://pastebin.com/0D700WPb
  12.  
  13. */
  14.  
  15. #include <iostream>
  16.  
  17. class Foo{
  18.     public:
  19.         void bar(){
  20.             std::cout << "Hello" << std::endl;
  21.         }
  22.         int doubleme (int input){
  23.        std::cout << "Doubling " << input << std::endl;
  24.        return 2*input;
  25.     }
  26.     void printfloat (double input){
  27.        std::cout << "Printing a float " << input << std::endl;
  28.     }
  29.    
  30. };
  31.  
  32.  
  33. extern "C" {
  34.     Foo* Foo_new(){ return new Foo(); }
  35.     void Foo_bar(Foo* foo){ foo->bar(); }
  36.     int  Foo_doubleme (Foo* foo, int input) {return foo->doubleme(input);}
  37.     void Foo_printfloat (Foo* foo, double input) {return foo->printfloat(input);}
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement