How does boost.python deal with Python 3? Is it Python 2 only?
相关问题
- how to define constructor for Python's new Nam
- Sorting 3 numbers without branching [closed]
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- How to compile C++ code in GDB?
Refer this to know how to build
boost
withpython
. It shows the way to build withpython2
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 toPreprocessor 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.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 forbjam
to find it):The inconsistent quoting is intended and seems to be required. With this I can build boost-python and use it as
Boost::python36
in myCMakeLists.txt
. Still, one issue remains: I have to link to python manually vizIf you get "error: No best alternative for /python_for_extension" be sure to have
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
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
in msys to generate a project-config.jam, but need to edit it to remove the "using python" and "/usr",..
Then the following
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.
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:
to your user-config.jam file.
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.
Then edit the python line (the last line) so that is says:
This is correct for Ubuntu 16.04. You can use pkg-config to find the correct include directory.
And you only need the first include directory.
Then build boost from scratch. (Sorry.) I install it to /usr/local
Now jump into the python example directory, and build the tutorial
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:
If you want to see the g++ commands that bjam is using, do this:
Here we see the problem, you need "-L/usr/includ/lib" just before "-lboost_python". So execute this to link the shared library correctly:
You may need to rerun ldconfig (or reboot)
And you are finally ready to go: