最近需要用python调用已经写好的c++函数,配了一下boostpython。
下载地址:https://www.boost.org/users/history/version_1_69_0.html
参考这篇博文:https://blog.csdn.net/bodybo/article/details/79962814
./bootstrap.sh --with-libraries=python --with-toolset=gcc
./b2 cflags=’-fPIC’ cxxflags=’-fPIC’ --with-python include="/home/3ysy/anaconda3/include/python3.6m/"
sudo ./b2 install
cd /usr/local/lib
sudo ln -s libboost_python-py36.so libboost_python3.so
sudo ln -s libboost_python-py36.a libboost_python3.a
将c++的函数封装,得到.so,在python中import调用
#include
#include
using namespace boost::python;
using namespace std;
void SayHello()
{
std::cout<<"hello, world!"<<std::endl;
}
void SayBye()
{ std::cout<<"good bye!"<<std::endl; }
BOOST_PYTHON_MODULE(hello) // generate python module hello
{ def("SayHello", SayHello);
def("SayBye", SayBye);
}
g++ hello.cpp -shared -fPIC -o hello.so -I ~/home/ysy/downlib/boost_1_69_0 -L ~/home/ysy/downlib/boost_1_69_0/stage/lib -lboost_python -I /home/ysy/anaconda3/envs/ysy/include/python3.6m
注意哦:把/home/ysy/downlib/boost_1_69_0 和/home/ysy/downlib/boost_1_69_0/stage/lib 换成自己解压的路径
可以看到所在文件夹生成了hellp.so文件
python3 -m pip uninstall osmium
rm -rf ~/.cache/pip/wheels/
rm /usr/lib/x86_64-linux-gnu/libboost_python.so
ln -s /usr/lib/x86_64-linux-gnu/libboost_python-py35.so /usr/lib/x86_64-linux-gnu/libboost_python.so
python3 -m pip install osmium