2011-04-04 92 views
17

boost.python如何處理Python 3?僅僅是Python 2嗎?Boost和Python 3.x

+1

看來,他們正在努力。 http://www.boost.org/doc/libs/1_46_0/libs/python/doc/news.html – mkaes 2011-04-04 14:04:36

回答

13

較新版本的Boost應該可以正常使用Python V3.x.這種支持已經加入前一段時間,我相信一個成功的代碼的谷歌暑期項目早在2009年

使用Python V3與升壓是通過添加例如適當配置構建系統的方式後:

using python : 3.1 : /your_python31_root ; 

到您的user-config.jam文件。

4

如果你得到「錯誤:/ python_for_extension沒有最好另類」一定要有

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

在家裏路徑only in user-config.jam和其他地方。 使用mingw(toolset = gcc)或MSVC(toolset = msvc)在窗口下編譯時使用雙反斜槓。 使用cmd進行編譯,而不是msys,並且如果您還安裝了python 2.7,請從該shell的PATH中刪除它。 首先做

bootstrap.bat gcc/msvc 

假設你必須通過PATH可用的GCC/MSVC工具(/備選項,但只使用一個,或告辭而去)

之後,你還可以做

booststrap.sh --with-bjam=b2 

在msys中生成一個project-config.jam,但需要編輯它才能刪除「using python」和「/ usr」,..

然後下面的

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

與靜態python快速入門示例不適合我,雖然我寧願沒有boost_python dll。

我沒有嘗試在Linux上,但它應該更直接。

0

請參閱this瞭解如何使用python構建boost。它展示了使用Visual Studio 10.0(2010)與python2一起構建的方法。但是我正在執行同一個項目,目前我正在使用該項目,並且它可以在python 3.5和Visual Studio 14.1(2017)上正常工作。

如果在構建python boost項目時獲得this error,只需在項目屬性中將BOOST_ALL_NO_LIB值添加到Preprocessor Definitions(在C \ C++>預處理器選項卡中)。
此外,不要忘記將boost .dll文件位置添加到您的系統路徑。

3

爲了做到這一點,libboostpython需要使用python3編譯。 這不適用於boost 1.58(它隨Ubuntu 16.04提供),因此請確保您下載最新的boost分發版。我剛剛用boost_1_64_0做了這個。

如上所述,在您的代碼中找到文件「user-config.jam」,並將其複製到$ HOME中。

cp /path/to/boost_1_64_0/tools/build/example/user-config.jam $HOME 

然後編輯蟒蛇行(最後一行),所以這是說:

using python : 3.5 : /usr/bin/python3 : /usr/include/python3.5m : /usr/lib ; 

這是對Ubuntu 16.04是正確的。您可以使用pkg-config來查找正確的include目錄。

[email protected] > pkg-config --cflags python3 
-I/usr/include/python3.5m -I/usr/include/x86_64-linux-gnu/python3.5m 

而你只需要第一個include目錄。

然後從頭開始建立提升。 (對不起)。我把它安裝到/ usr /本地

cd /path/to/boost_1_64_0 
./bootstrap.sh --prefix=/usr/local 
./b2 
sudo ./b2 install 

現在跳進Python的例子目錄,並建立教程

cd /path/to/boost_1_64_0/libs/python/example/tutorial 
bjam 

這將無法正確生成,如果你有一個系統安裝的boost,因爲在底層,bjam使用g ++參數「-lboost」鏈接到libboostpython。但是,在Ubuntu 16.04上,這隻會發現「/usr/lib/x86_64-linux-gnu/libboost_python-py27.so.1.58.0」,然後python綁定將無法加載。事實上,你會得到他的錯誤:

ImportError: /usr/lib/x86_64-linux-gnu/libboost_python-py27.so.1.58.0: undefined symbol: PyClass_Type 

如果你想看到++命令,bjam的使用克,這樣做:

[email protected] > 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 

在這裏,我們看到了問題,你需要「 - L/usr/includ/lib「放在」-lboost_python「之前。所以執行這個共享庫正確鏈接:

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 

您可能需要重新運行ldconfig命令(或重啓)

sudo ldconfig 

而且你終於準備好了:

[email protected] > 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() 
+0

這對我完全有幫助。讓我的項目使用自定義安裝的所有東西(gcc,python3,boost)構建的關鍵是編輯我的'project-config.jam'文件,如您所示。即調整'using python:...'行以列出python3解釋器的每個二進制可執行文件,它的include文件夾和lib目錄。重新構建提升和安裝後,我的項目正確使用python 3構建。謝謝! – ofloveandhate 2017-10-11 15:43:59