boost python 任何3.x版本_Boost and Python 3.x

libboostpython needs to be built with python3 in order to do this. This doesn't work with boost 1.58 (which comes with Ubuntu 16.04), so make sure you download the latest boost distribution. I just did this with boost_1_64_0.

As mentioned above, find the file "user-config.jam" in you boost code distribution, and copy it to $HOME. cp /path/to/boost_1_64_0/tools/build/example/user-config.jam $HOME

Then edit the python line (the last line) so that is says: using python : 3.5 : /usr/bin/python3 : /usr/include/python3.5m : /usr/lib ;

This is correct for Ubuntu 16.04. You can use pkg-config to find the correct include directory. user@computer > pkg-config --cflags python3 -I/usr/include/python3.5m -I/usr/include/x86_64-linux-gnu/python3.5m

And you only need the first include directory.

Then build boost from scratch. (Sorry.) I install it to /usr/local cd /path/to/boost_1_64_0 ./bootstrap.sh --prefix=/usr/local ./b2 sudo ./b2 install

Now jump into the python example directory, and build the tutorial cd /path/to/boost_1_64_0/libs/python/example/tutorial bjam

This will not build correctly if you have a system install of boost, because, under the hood, bjam is linking to libboostpython using the g++ parameter "-lboost". But, on Ubuntu 16.04, this will just go and find "/usr/lib/x86_64-linux-gnu/libboost_python-py27.so.1.58.0", and then the python bindings will fail to load. In fact, you'll get his error: ImportError: /usr/lib/x86_64-linux-gnu/libboost_python-py27.so.1.58.0: undefined symbol: PyClass_Type

If you want to see the g++ commands that bjam is using, do this: user@computer > bjam -d2 -a | grep g++ g++ -ftemplate-depth-128 -O0 -fno-inline -Wall -g -fPIC -I/usr/include/python3.5m -c -o "hello.o" "hello.cpp" g++ -o hello_ext.so -Wl,-h -Wl,hello_ext.so -shared -Wl,--start-group hello.o -Wl,-Bstatic -Wl,-Bdynamic -lboost_python -ldl -lpthread -lutil -Wl,--end-group

Here we see the problem, you need "-L/usr/includ/lib" just before "-lboost_python". So execute this to link the shared library correctly: g++ -o hello_ext.so -Wl,-h -Wl,hello_ext.so -shared -Wl,--start-group hello.o -Wl,-Bstatic -Wl,-Bdynamic -L/usr/local/lib -lboost_python -ldl -lpthread -lutil -Wl,--end-group

You may need to rerun ldconfig (or reboot) sudo ldconfig

And you are finally ready to go: user@computer > python3 Python 3.5.2 (default, Nov 17 2016, 17:05:23) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import hello_ext >>> hello_ext.greet() 'hello, world' >>> exit()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值