반응형
ctypes class ][ python에서 c++ class 사용하기
■ git
# # https://github.com/SpeeDr00t/code/tree/master/python/0x04-ctypes/0x01-helloworld # #
■ hello.cpp
// // // // write by kyoung chip , jang // // g++ -c -fPIC hello.cpp -o hello.o // g++ -shared -Wl,-soname,libhello.so -o libhello.so hello.o // // #include <iostream> #include <vector> #include <string> #include <iterator> using namespace std; class CHello { private: vector<string> m_vec; public: void print() { vector<string>::iterator it; for( it = m_vec.begin(); it != m_vec.end(); it ++ ) { cout << (*it) << endl; } } void push_back( string s ) { m_vec.push_back( s ); } }; extern "C" { CHello* CHello_new() { return new CHello(); } void CHello_print( CHello * f) { f->print(); } void CHello_push_back( CHello * f , char * s ) { f->push_back(s); } }
■ hello.py
# # # write by kyoung chip , jang # # python 3.6 # # from ctypes import cdll class CHello(object): def __init__( self ): self.lib = cdll.LoadLibrary('./libhello.so') self.obj = self.lib.CHello_new() def printOut( self ): self.lib.CHello_print( self.obj ) def push_back( self , s ) : self.lib.CHello_push_back( self.obj, s ) if __name__ == '__main__': f = CHello() f.push_back("-------------------------------------") f.push_back(" hello world ") f.push_back("-------------------------------------") f.printOut()
반응형
'Python > 0x05-ctypes' 카테고리의 다른 글
c에서 python 호출하기 ][ test (0) | 2019.09.27 |
---|