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

来源:互联网 发布:2016淘宝买家可以贷款 编辑:程序博客网 时间:2024/05/20 07:36

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.so

usec.py

from 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()直接调用方法


0 0
原创粉丝点击