您的位置:首页 > 编程语言 > C语言/C++

python下面通过ctypes模块调用c++库的方法

2016-12-22 20:10 766 查看
foo.cpp:#include <iostream>class Foo{public:void bar(){std::cout << "Hello" << std::endl;}};int test() {std::cout<<"hello world"<<std::endl;return 0;}extern "C" {Foo* Foo_new(){return new Foo();}void Foo_bar(Foo* foo){foo->bar();}int Test() {return test();}}通过 g++ -o libfoo.so -shared -fPIC foo.cpp  生产foo.sousec.pyfrom ctypes import cdlllib = cdll.LoadLibrary('./libfoo.so')#class Foo(object):# def __init__(self):# self.obj = lib.Foo_new()# def bar(self):# lib.Foo_bar(self.obj)lib.Test()#f = Foo()#f.bar() #and you will see "Hello" on the screen
注释掉的是调用c++类的方法,lib.Test()直接调用方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: