[英]Boost and Python 3.x
boost.python如何處理Python 3? 只有Python 2嗎?
較新版本的 Boost 應該適用於 Python V3.x。 這種支持是在很久以前添加的,我相信在 2009 年成功的 Google Summer of Code 項目之后。
將 Python V3 與 Boost 結合使用的方法是通過添加以下內容來正確配置構建系統:
using python : 3.1 : /your_python31_root ;
到您的 user-config.jam 文件。
libboostpython 需要用 python3 構建才能做到這一點。 這不適用於 boost 1.58 (隨 Ubuntu 16.04 一起提供),因此請確保下載最新的 boost 發行版。 我只是用 boost_1_64_0 做到了這一點。
如上所述,在您的 boost 代碼分發中找到文件“user-config.jam”,並將其復制到 $HOME。
cp /path/to/boost_1_64_0/tools/build/example/user-config.jam $HOME
然后編輯 python 行(最后一行),這樣說:
using python : 3.5 : /usr/bin/python3 : /usr/include/python3.5m : /usr/lib ;
這對於 Ubuntu 16.04 來說是正確的。 您可以使用 pkg-config 找到正確的包含目錄。
user@computer > pkg-config --cflags python3
-I/usr/include/python3.5m -I/usr/include/x86_64-linux-gnu/python3.5m
並且您只需要第一個包含目錄。
然后從頭開始構建提升。 (抱歉。)我將它安裝到 /usr/local
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 正在使用的 g++ 命令,請執行以下操作:
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
在這里我們看到了問題,在“-lboost_python”之前需要“-L/usr/includ/lib”。 因此,執行此操作以正確鏈接共享庫:
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
你終於准備好了:
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()
如果您收到“錯誤:/python_for_extension 沒有最佳替代方案”,請確保
using python : 3.4 : C:\\Python34 : C:\\Python34\\include : C:\\Python34\\libs ;
僅在您的主路徑中的 user-config.jam中,而沒有其他地方。 在 windows 下用 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 上嘗試過,但那里應該更簡單。
是的,這個問題太老了,但我必須做一些這里的任何答案中都沒有指定的事情(盡管它是根據一些建議建立的),所以我會很快記下我的整個過程:
tar --bzip2 -xf boost_1_68_0.tar.bz2
(你想要臨時文件夾的位置)cd boost_1_68_0
./bootstrap.sh --with-python-version=3.6 --prefix=/usr/local
./b2
sudo ./bjam install
cp tools/build/example/user-config.jam $HOME
,然后修改這個文件的內容說using python : 3.6 : /usr/bin/python3 : /usr/include/python3.6m : /usr/lib ;
(或任何適合您環境的文件夾) 鑒於此 C++ 源文件BoostPythonHelloWorld.cpp
:
#include <boost/python.hpp>
char const* say_hi()
{
return "Hi!";
}
BOOST_PYTHON_MODULE(BoostPythonHelloWorld)
{
boost::python::def("say_hi", say_hi);
}
這個 Python 腳本BoostPythonHelloWorld.py
:
import BoostPythonHelloWorld
print(BoostPythonHelloWorld.say_hi())
它可以這樣編譯和運行:
gcc -c -fPIC -I/path/to/boost_1_68_0 -I/usr/include/python3.6 /other_path/to/BoostPythonHelloWorld.cpp
gcc -shared -Wall -Werror -Wl,--export-dynamic BoostPythonHelloWorld.o -L/path/to/boost_1_68_0/stage/lib -lboost_python36 -o BoostPythonHelloWorld.so
python3 BoostPythonHelloWorld.py
對我來說不同的部分是-Wl,--export-dynamic BoostPythonHelloWorld.o
,我在其他任何地方都沒有看到過,並且在我添加之前,我收到了有關未定義符號的 Python 錯誤。
希望這可以幫助某人!
您甚至可以通過
./bootstrap.sh --with-python=<path to your python binary>
例如
./bootstrap.sh --with-python=python3
對於您系統的 python3 或
./bootstrap.sh --with-python=$VIRTUAL_ENV/bin/python
對於當前活動的虛擬環境 python 的 python 版本。
當 Python 的路徑中包含空格時,您會遇到很多麻煩。 經過大量的反復試驗,我終於設法得到了一些有用的東西。 看我的user-config.jam
(它必須在我的主目錄中, bjam
才能找到它):
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
;
不一致的引用是有意的,似乎是必需的。 有了這個,我可以構建 boost-python 並在我的CMakeLists.txt
Boost::python36
其用作Boost::python36
。 盡管如此,仍然存在一個問題:我必須手動鏈接到 python,即
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")
在我的例子中添加“使用 Python:3 等”。 進入我的主目錄中的user-config.jam無效。 我不得不將該行添加到project-config.jam中,它位於解壓縮的 boost 的根目錄中。
具體來說,這條線是:
使用 python: 3.9: /usr/bin/python3: /usr/include/python3.9: /usr/lib;
boost 的版本是 1_78_0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.