notes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 whereis python3 sudo apt install python3-dev tar -xzvf boost_1_76_0.tar.gz rm boost_1_76_0.tar.gz cd boost_1_76_0/sudo bootstrap.sh vim project_config.jam using python : 3.5 : /usr/bin/python3.5m : /usr/include/python3.5m : /usr/lib/python3.5 ; sudo ./b2 sudo ./b2 install --with-python mkdir demo_boost cd demo_boostvim test.cpp
1 2 3 4 5 6 7 8 9 10 11 #include <boost/python.hpp> char const * greet () { return "hello, world" ; } BOOST_PYTHON_MODULE(test) { using namespace boost::python; def("greet" , greet); }
1 2 3 4 5 6 7 8 9 sudo vim /etc/ld.so.conf /usr/local /lib sudo ldconfig g++ -I/usr/include/python3.5m -fPIC test.cpp -lboost_python35 -shared -o test.so vim test_py.py
1 2 3 4 5 import testprint(dir(test)) print(test.greet())
另一个例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include <boost/python.hpp> #include <iostream> #include <string> class Bonjour { std ::string m_msg; public : Bonjour(std ::string msg) :m_msg(msg) { } ~Bonjour() { std ::cout << "destructed " << this << std ::endl ; } void greet () { std ::cout << m_msg << std ::endl ; } void set_msg (std ::string msg) { this ->m_msg = msg; } std ::string get_msg () const { return m_msg; } }; BOOST_PYTHON_MODULE(demo) { using namespace boost::python; class_<Bonjour>("Bonjour" , init<std ::string >()) .def("greet" , &Bonjour::greet) .add_property("msg" , &Bonjour::get_msg, &Bonjour::set_msg); }
references tar解压缩命令 :[linux tar.gz zip 解压缩 压缩命令
安装与编译 :linux下安装boost python详解
编译 :Linux 编译boost (for python3)
动态库问题 :cannot open shared object file: No such file or directory解决方法