python调用c++代码,.so库的编译

用python作为C++核心算法的wrapper,使用起来非常方便,将c++代码编译成python可以import的.so库过程如下:

1. 安装boost: http://www.boost.org/

2. 写封装接口函数sample.cc:

#include 
#include 

//函数模块
char const* sayHello()
{
    return "Hello from boost::python";
}

//封装模块
BOOST_PYTHON_MODULE(hello)
{
     using namespace boost::python;
     def("sayHello",sayHello);
}

3. 编译.so 库:

g++ sample.cc -o hello.so -shared -fPIC -I/usr/local/include/python2.7 -lboost_python


4.python调用:

import hello

hello.sayHello()



你可能感兴趣的:(程序调试配置)