Boost and Python 3.x

2019-01-17 01:03发布

How does boost.python deal with Python 3? Is it Python 2 only?

5条回答
ゆ 、 Hurt°
2楼-- · 2019-01-17 01:28

Refer this to know how to build boost with python. It shows the way to build with python2 with Visual Studio 10.0 (2010). But I go through the same procedure for a project that I am currently working on and it works fine with python 3.5 and Visual Studio 14.1 (2017).

If you get this error when building your python boost project, just add BOOST_ALL_NO_LIB value to Preprocessor Definitions (inside C\C++ > preprocessor tab) in your project properties.
And also, do not forget to add boost .dll files location to your system path.

查看更多
放我归山
3楼-- · 2019-01-17 01:28

When the path to Python contains spaces, you will be in for quite a ride. After a whole lot of trial and error, I finally managed to get something that works. Behold my user-config.jam (which has to be in my home directory for bjam to find it):

import toolset : using ;

using python : 3.6
         : \"C:\\Program\ Files\ (x86)\\Microsoft\ Visual\ Studio\\Shared\\Python36_64\\python.exe\"
         : C:\\Program\ Files\ (x86)\\Microsoft\ Visual\ Studio\\Shared\\Python36_64\\include
         : C:\\Program\ Files\ (x86)\\Microsoft\ Visual\ Studio\\Shared\\Python36_64\\libs
         ;

The inconsistent quoting is intended and seems to be required. With this I can build boost-python and use it as Boost::python36 in my CMakeLists.txt. Still, one issue remains: I have to link to python manually viz

target_link_libraries(MyTarget
    Boost::boost Boost::python36
"C:/Program Files (x86)/Microsoft Visual Studio/Shared/Python36_64/libs/python36.lib")
target_include_directories(MyTarget PRIVATE
    "C:/Program Files (x86)/Microsoft Visual Studio/Shared/Python36_64/include")
查看更多
孤傲高冷的网名
4楼-- · 2019-01-17 01:35

If you get "error: No best alternative for /python_for_extension" be sure to have

using python : 3.4 : C:\\Python34 : C:\\Python34\\include : C:\\Python34\\libs ;

only in user-config.jam in your home path and nowhere else. Use double backslashes when compiling under windows with mingw (toolset=gcc) or MSVC (toolset=msvc). Compile with cmd, not msys, and if you also have python 2.7 installed remove that from PATH in that shell. First do

bootstrap.bat gcc/msvc

assuming you have the gcc/msvc tools available via PATH (/ for the alternatives, but use only one, or leave away)

Afterward you can also do

booststrap.sh --with-bjam=b2

in msys to generate a project-config.jam, but need to edit it to remove the "using python" and "/usr",..

Then the following

b2 variant=debug/shared link=static/shared toolset=gcc/msvc > b2.log

With static the python quickstart examples did not work for me, although I would prefer to do without the boost_python dll.

I did not try on linux, but it should be more straightforward there.

查看更多
太酷不给撩
5楼-- · 2019-01-17 01:39

Newer versions of Boost should work fine with Python V3.x. This support has been added quite some time ago, I believe after a successful Google Summer of Code project back in 2009.

The way to use Python V3 with Boost is to properly configure the build system by adding for instance:

using python : 3.1 : /your_python31_root ;

to your user-config.jam file.

查看更多
三岁会撩人
6楼-- · 2019-01-17 01:43

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()
查看更多
登录 后发表回答