Contents
目錄
Boost.Python requires Python 2.21 or newer.
Boost.Python要求Python 2.21 或 更高版。
There are two basic models for combining C++ and Python:
組合C++和Python有兩種基本模式:
The key distinction between extending and embedding is the locationof the C++ main() function: in the Python interpreter executable,or in some other program, respectively. Note that even whenembedding Python in another program, extension modules are oftenthe best way to make C/C++ functionality accessible to Pythoncode, so the use of extension modules is really at the heart ofboth models.
擴展和內嵌的關鍵性區別是,C++ main()函數的位置是在Python解譯器程序中,還是在其他程序中? 注意,即使是在程序中內嵌Python,擴展模塊往往是Python代碼利用C/C++功能最好的方式,所以,這兩種模式實際上是以擴展模塊的使用為中心的。
Except in rare cases, extension modules are built asdynamically-loaded libraries with a single entry point, which meansyou can change them without rebuilding either the other extensionmodules or the executable containing main().
除了極少數情況,擴展模塊是按具有單一入口點的動態庫構建的,這意味著,你可以修改它們,而不必重建其它擴展模塊或包含main()的執行程序。
There is no need to "install Boost" in order to get started usingBoost.Python. These instructions use Boost.Build projects,which will build those binaries as soon as they're needed. Yourfirst tests may take a little longer while you wait forBoost.Python to build, but doing things this way will save you fromworrying about build intricacies like which library binaries to usefor a specific compiler configuration and figuring out the rightcompiler options to use yourself.
使用Boost.Python不需要“安裝Boost”。這里的教程利用了Boost.Build項目,它會在需要時自動構建那些二進制庫。你的第一個測試,可能需要較長的時間,你需要等待Boost.Python的構建,但這樣做,讓你不必擔憂錯綜復雜的構建,如,某個特定編譯器配置該使用哪個二進制庫,也讓你免于自己設置編譯器選項。
Note
注
Of course it's possible to use other build systems tobuild Boost.Python and its extensions, but they are notofficially supported by Boost. Moreover 99% of all "I can'tbuild Boost.Python" problems come from trying to use anotherbuild system without first following these instructions.
當然,也可以使用其他的編譯系統,來構建Boost.Python及其擴展,但它們不是Boost正式支持的。所有“我無法編譯Boost.Python”的問題,99%以上是因為試圖使用另一種編譯系統,而沒有先按這個教程做。
If you want to use another system anyway, we suggest that youfollow these instructions, and then invoke bjam with the
-a -ofilename
options to dump the build commands it executes to a file, soyou can see what your alternate build system needs to do.
如果你想使用另一個系統,我們建議你先遵循這些指示,然后用以下參數調用bjam,
來輸出它對文件執行的構建命令,這樣你就可以看到你的編譯系統需要做些什么。
Get Boost; see sections 1 and 2 [Unix/Linux, Windows] of theBoost Getting Started Guide.
獲取Boost;見Boost入門指南第1和第2節[Unix/Linux,Windows]。
Get the bjam build driver. See section 5 [Unix/Linux,Windows] of the Boost Getting Started Guide.
獲取bjam構建驅動器。見Boost入門指門第5節[Unix/Linux,Windows]。
cd into the libs/python/example/quickstart/ directory of yourBoost installation, which contains a small example project.
cd到Boost安裝目錄下的libs/python/example/quickstart/,那里有個小的示例工程。
Invoke bjam. Replace the "stage" argument from theexample invocation from section 5 of the Getting StartedGuide with "test," to build all the test targets. Also addthe argument "--verbose-test" to see the output generated bythe tests when they are run.
調用bjam。將入門指南第5節例示的調用命令中的“stage”參數替換為“test”,來構建所有測試目標。 同時添加參數“--verbose-test”來查看測試運行時生成的輸出。
On Windows, your bjam invocation might look something like:
在Windows上,你的bjam調用可能像這樣:
C:\boost_1_34_0\…\quickstart> bjam toolset=msvc --verbose-test test
and on Unix variants, perhaps,
而在各種Unix上,可能是:
~/boost_1_34_0/…/quickstart$ bjam toolset=gcc --verbose-test test
Note to Windows Users
Windows用戶注意
For the sake of concision, the rest of this guide will useunix-style forward slashes in pathnames instead of thebackslashes with which you may be more familiar. The forwardslashes should work everywhere except in Command Promptwindows, where you should use backslashes.
為求簡潔,本指南的其余部分將在路徑中使用unix風格的斜杠,而不是你可能更熟悉的反斜杠。斜杠應該到處可用,除了命令提示符窗口,在那里您應該使用反斜杠。
If you followed this procedure successfully, you will have built anextension module called extending and tested it by running aPython script called test_extending.py. You will also havebuilt and run a simple application called embedding that embedspython.
如果你能成功地遵循這個過程,你將構建出一個擴展模塊,名為extending,并運行一個名為test_extending.py的Python腳本測試它。你也將構建并運行一個內嵌python的簡單應用程序,名為embedding。
If you're seeing lots of compiler and/or linker error messages,it's probably because Boost.Build is having trouble finding yourPython installation. You might want to pass the--debug-configuration option to bjam the first few timesyou invoke it, to make sure that Boost.Build is correctly locatingall the parts of your Python installation. If it isn't, considerConfiguring Boost.Build as detailed below.
如果你見到大量編譯或連接的錯誤信息,那可能是因為Boost.Build無法找到你的Python安裝。你也許要在最初幾次調用bjam時,傳入--debug-configuration參數,以確保Boost.Build正確定位了所有Python組件。 如果不是,請考慮配置Boost.Build。
If you're still having trouble, Someone on one of the followingmailing lists may be able to help:
如果仍有問題,請向以下郵件列表尋求幫助:
Rejoice! If you're new to Boost.Python, at this point it might bea good idea to ignore build issues for a while and concentrate onlearning the library by going through the tutorial and perhapssome of the reference documentation, trying out what you'velearned about the API by modifying the quickstart project.
恭喜!如果您是Boost.Python新手,此時,最好暫時不理會構建問題,而是集中精力去學習庫,盡快學完教程,然后可能是參考文檔,同時通過修改快速入門工程,試用你所學到的API。
If you're content to keep your extension module forever in onesource file called extending.cpp, inside your Boostdistribution, and import it forever as extending, then you canstop here. However, it's likely that you will want to make a fewchanges. There are a few things you can do without having to learnBoost.Build in depth.
如果你滿足于此,擴展模塊的源文件永遠是extending.cpp,位于Boost發布目錄中,并永遠按extending導入,那么你就可以到此為止。 不過很可能是,你會想作一些變動。有許多事你可以做,而無須深入學習Boost.Build。
The project you just built is specified in two files in the currentdirectory: boost-build.jam, which tells bjam where it canfind the interpreted code of the Boost build system, andJamroot, which describes the targets you just built. Thesefiles are heavily commented, so they should be easy to modify.Take care, however, to preserve whitespace. Punctuation such as; will not be recognized as intended by bjam if it is notsurrounded by whitespace.
你剛剛構建的工程,是由當前目錄下的兩個文件詳細說明的:boost-build.jam(它告訴bjam,哪里可以找到Boost編譯系統的解釋代碼),和Jamroot(它描述了你剛剛構建的目標)。這些文件具有詳細的注釋,所以它們應該很容易修改。不過小心,要保留空白字符。如;這樣的標點符號,如果周圍沒有空白字符,bjam將無法識別。
You'll probably want to copy this project elsewhere so you canchange it without modifying your Boost distribution. To do that,simply
你可能想把該工程復制到其他位置,這樣你就可以修改它,而不必在Boost發布目錄中更改。要做到這一點,只需
For example, if you moved the project from/home/dave/boost_1_34_0/libs/python/example/quickstart to/home/dave/my-project, you could change the first path inboost-build.jam from
例如,如果你的工程從/home/dave/boost_1_34_0/libs/python/example/quickstart移至/home/dave/my-project,你可以更改boost-build.jam中的第一個路徑,從
../../../../tools/build/v2
to
改為
/home/dave/boost_1_34_0/tools/build/v2
and change the first path in Jamroot from
并且,將Jamroot中的第一個路徑從
../../../..
/home/dave/boost_1_34_0
The names of additional source files involved in building yourextension module or embedding application can be listed inJamroot right alongside extending.cpp or embedding.cpprespectively. Just be sure to leave whitespace around eachfilename:
構建擴展模塊或內嵌應用程序時,涉及的其他源文件的名字,可以在Jamroot中列出,分別列于extending.cpp或embedding.cpp旁邊。只是每個文件名周圍,務必留下空白字符:
… file1.cpp file2.cpp file3.cpp …
Naturally, if you want to change the name of a source file you cantell Boost.Build about it by editing the name in Jamroot.
當然,如果你想要更改源文件名,你可以編輯Jamroot中的文件名,來告訴Boost.Build。
The name of the extension module is determined by two things:
擴展模塊的名字,由兩個地方決定:
To change the name of the extension module from extending tohello, you'd edit Jamroot, changing
要將擴展模塊名從extending改為hello,你需要編輯Jamroot,將
python-extension extending : extending.cpp ;
python-extension hello : extending.cpp ;
and you'd edit extending.cpp, changing
還需要編輯extending.cpp,將
BOOST_PYTHON_MODULE(extending)
BOOST_PYTHON_MODULE(hello)
Since Boost.Python is a separately-compiled (as opposed toheader-only) library, its user relies on the services of aBoost.Python library binary.
由于Boost.Python是一個單獨編譯的庫(有別于純頭文件庫),它的用戶需要依賴Boost.Python二進制庫的服務。
If you need a regular installation of the Boost.Python librarybinaries on your system, the Boost Getting Started Guide willwalk you through the steps of creating one. If building binariesfrom source, you might want to supply the --with-pythonargument to bjam (or the --with-libraries=python argumentto configure), so only the Boost.Python binary will be built,rather than all the Boost binaries.
如果你需要在系統上安裝一個常規的Boost.Python二進制庫,Boost入門指南將帶你一步一步創建它。如果從源文件構建二進制,你也許要向bjam提供--with-python參數(或向configure提供--with-libraries=python參數),這樣就會僅僅構建Boost.Python二進制,而不是所有的Boost二進制。
As described in the Boost.Build reference manual, a file calleduser-config.jam in your home directory6 is used tospecify the tools and libraries available to the build system. Youmay need to create or edit user-config.jam to tell Boost.Buildhow to invoke Python, #include its headers, and link with itslibraries.
正如Boost.Build參考手冊所述,在你的主目錄6下,有一個user-config.jam文件,用來指定編譯系統可用的工具和庫。您可能需要創建或編輯user-config.jam,來告訴Boost.Build如何調用Python,#include它的頭文件,并鏈接它的庫。
Users of Unix-Variant OSes
類Unix OS用戶
If you are using a unix-variant OS and you ran Boost'sconfigure script, it may have generated auser-config.jam for you.4 If your configure/make sequence was successful and Boost.Python binarieswere built, your user-config.jam file is probably alreadycorrect.
如果你使用的是類Unix操作系統,并且你運行了Boost configure腳本,它可能已經為你生成了一個user-config.jam4。如果你configure/make就能成功構建Boost.Python,你的user-config.jam文件可能已經是正確的了。
If you have one fairly "standard" python installation for yourplatform, you might not need to do anything special to describe it. Ifyou haven't configured python in user-config.jam (and you don'tspecify --without-python on the Boost.Build command line),Boost.Build will automatically execute the equivalent of
如果你的平臺有一個相當“標準”的python安裝,你可能不需要對它做特別描述。如果你沒有在user-config.jam中配置python(并且沒有在Boost.Build命令行中指定--without-python),Boost.Build將自動執行以下等效語句
import toolset : using ;using python ;
which automatically looks for Python in the most likely places.However, that only happens when using the Boost.Python project file(e.g. when referred to by another project as in the quickstartmethod). If instead you are linking against separately-compiledBoost.Python binaries, you should set up a user-config.jam filewith at least the minimal incantation above.
這會讓它在最有可能的地方自動尋找Python。 不過,這只會發生在當使用Boost.Python項目文件時(例如,當被另一個項目引用時,如快速入門所示方法)。反之,如果你鏈接單獨編譯的Boost.Python二進制,你應該建立一個user-config.jam文件,至少包含以上最少量的咒語。
If you have several versions of Python installed, or Python isinstalled in an unusual way, you may want to supply any or all ofthe following optional parameters to using python.
如果你安裝了多個Python,或Python以一個不尋常的方式安裝,你可能要向using python提供下列部分或全部可選參數。
Note that in the examples below, case and especially whitespace aresignificant.
注意,在下面的例子中,大小寫,尤其是空白字符是有意義的。
If you have both python 2.5 and python 2.4 installed,user-config.jam might contain:
如果你同時安裝了python 2.5和python 2.4,user-config.jam可能包含:
using python : 2.5 ; # Make both versions of Python availableusing python : 2.4 ; # To build with python 2.4, add python=2.4 # to your command line.
The first version configured (2.5) becomes the default. To buildagainst python 2.4, add python=2.4 to the bjam command line.
第一個配置的版本(2.5)將成為默認值。如果要使用python 2.4構建,請在bjam命令行中添加python=2.4。
If you have python installed in an unusual location, you mightsupply the path to the interpreter in the cmd-or-prefixparameter:
如果你把python安裝在一個不尋常的位置,你可以在cmd-or-prefix參數中提供解釋器的路徑:
using python : : /usr/local/python-2.6-beta/bin/python ;
If you have a separate build of Python for use with a particulartoolset, you might supply that toolset in the conditionparameter:
如果一個特定工具集使用了一個單獨構建的Python,你可以在condition參數中提供該工具集。
using python ; # use for most toolsets# Use with Intel C++ toolsetusing python : # version : c:\\Devel\\Python-2.5-IntelBuild\\PCBuild\\python # cmd-or-prefix : # includes : # libraries : <toolset>intel # condition ;
If you have downloaded the Python sources and built both thenormal and the "python debugging" builds from source onWindows, you might see:
如果你下載了Python源碼并且在Windows上自己構建了python和python調試版,你可能會看到:
using python : 2.5 : C:\\src\\Python-2.5\\PCBuild\\python ;using python : 2.5 : C:\\src\\Python-2.5\\PCBuild\\python_d : # includes : # libs : <python-debugging>on ;
You can set up your user-config.jam so a bjam built under Windowscan build/test both Windows and Cygwin python extensions. Just pass<target-os>cygwin in the condition parameterfor the cygwin python installation:
你可以設置user-config.jam,讓Windows上的bjam能夠構建和測試兩個版本的python擴展,既有Windows版,也有Cygwin版。只需在condition參數中,為cygwin的python安裝傳入<target-os>cygwin:
# windows installationusing python ;# cygwin installationusing python : : c:\\cygwin\\bin\\python2.5 : : : <target-os>cygwin ;
when you put target-os=cygwin in your build request, it should buildwith the cygwin version of python:5
當你在構建請求中加入target-os=cygwin,它就應該使用cygwin版本的python進行構建:5
bjam target-os=cygwin toolset=gcc
This is supposed to work the other way, too (targeting windowspython with a Cygwin bjam) but it seems as though the support inBoost.Build's toolsets for building that way is broken at thetime of this writing.
另一種方式應該也行(使用Cygwin上的bjam,以windows python為目標),但在本文寫作之時,Boost.Build的工具集對那種構建方式的支持好像有問題。
Note that because of the way Boost.Build currently selects targetalternatives, you might have be very explicit in your buildrequests. For example, given:
注意,在Boost.Build目前選擇不同目標的方式下,可能你的編譯請求必須非常明確。舉例來說,如有:
using python : 2.5 ; # a regular windows buildusing python : 2.4 : : : : <target-os>cygwin ;
building with
以
bjam target-os=cygwin
will yield an error. Instead, you'll need to write:
構建會產生一個錯誤。你需要這樣寫才行:
bjam target-os=cygwin/python=2.4
If—instead of letting Boost.Build construct and link with the rightlibraries automatically—you choose to use a pre-built Boost.Pythonlibrary, you'll need to think about which one to link with. TheBoost.Python binary comes in both static and dynamic flavors. Takecare to choose the right flavor for your application.2
如果不是任由Boost.Build自動構建和鏈接正確的庫,而是選擇使用預建的Boost.Python庫,你需要考慮一下鏈接哪個庫。Boost.Python二進制庫同時具有靜態和動態庫。請為您的應用小心選擇正確的庫。2
The dynamic library is the safest and most-versatile choice:
動態庫是最安全和最通用的選擇:
It might be appropriate to use the static Boost.Python library inany of the following cases:
下列任一情況下,可能適合使用靜態的Boost.Python庫:
Python can be built in a special "python debugging" configurationthat adds extra checks and instrumentation that can be very usefulfor developers of extension modules. The data structures used bythe debugging configuration contain additional members, so aPython executable built with python debugging enabled cannot beused with an extension module or library compiled without it, andvice-versa.
Python可以用一個特殊的配置,“Python調試”,進行構建,它增加了額外的檢查和測試,對擴展模塊的開發者非常有用。調試配置所使用的數據結構,包含了附加的成員,因此,用python調試模式構建的Python可執行程序,不能使用非調試模式編譯的擴展模塊或庫,反之亦然。
Since pre-built "python debugging" versions of the Pythonexecutable and libraries are not supplied with most distributionsof Python,7 and we didn't want to force our usersto build them, Boost.Build does not automatically enable pythondebugging in its debug build variant (which is the default).Instead there is a special build property calledpython-debugging that, when used as a build property, willdefine the right preprocessor symbols and select the rightlibraries to link with.
由于在大多數Python發行版中,沒有提供預建的“python調試”版的Python可執行程序和庫7,而且我們不想強迫我們的用戶去構建它們,所以,在Boost.Python的debug版中(默認是debug版),沒有自動打開python調試模式,作為替代,有一個特別的構建屬性,稱為python-debugging,使用該構建屬性時,會定義正確的預處理符號,并選擇正確的庫進行鏈接。
On unix-variant platforms, the debugging versions of Python's datastructures will only be used if the symbol Py_DEBUG is defined.On many windows compilers, when extension modules are built withthe preprocessor symbol _DEBUG, Python defaults to forcelinking with a special debugging version of the Python DLL. Sincethat symbol is very commonly used even when Python is not present,Boost.Python temporarily undefines _DEBUG when Python.his #included from boost/python/detail/wrap_python.hpp - unlessBOOST_DEBUG_PYTHON is defined. The upshot is that if you want"python debugging" and you aren't using Boost.Build, you should makesure BOOST_DEBUG_PYTHON is defined, or python debugging will besuppressed.
在類Unix平臺上,僅當定義了Py_DEBUG符號時,才會使用調試版Python的數據結構。在許多Windows編譯器上,當擴展模塊用預處理符號_DEBUG構建時,Python默認會強制鏈接一個特殊的,調試版本的Python DLL。由于這個符號很常用(甚至沒有Python時),所以,在boost/python/detail/wrap_python.hpp中包含Python.h時,Boost.Python暫時取消了_DEBUG定義——除非定義了BOOST_DEBUG_PYTHON。其結果是,如果你想要“Python調試”,并且你沒有用Boost.Build,你務必定義BOOST_DEBUG_PYTHON,不然python調試會被禁用。
To run the full test suite for Boost.Python, invoke bjam in thelibs/python/test subdirectory of your Boost distribution.
在Boost的libs/python/test子目錄下, 調用bjam,以運行Boost.Python完整的測試包。
If you are using a version of Python prior to 2.4.1 with a MinGWprior to 3.0.0 (with binutils-2.13.90-20030111-1), you will need tocreate a MinGW-compatible version of the Python library; the oneshipped with Python will only work with a Microsoft-compatiblelinker. Follow the instructions in the "Non-Microsoft" section ofthe "building Extensions: Tips And Tricks" chapter in InstallingPython Modules to create libpythonXX.a, where XXcorresponds to the major and minor version numbers of your Pythoninstallation.
如果你正在使用Python 2.4.1,MinGW 3.0.0(和binutils-2.13.90-20030111-1)之前的版本,您將需要創建一個與MinGW兼容的Python庫;Python發行隨帶的庫只能用于微軟兼容的連接器。請轉到安裝Python模塊,“構建擴展:提示和技巧”一章,“非微軟”一節,按照指示來創建libpythonXX.a,其中XX為你的Python安裝的主要和次要版本號。
注意,盡管我們在Python 2.2上測試過Boost.Python的早期版本,并且我們認為我們沒有破壞兼容性,但本次發行的Boost.Python可能沒有在Python 2.4以前的版本上測試過,所以我們不是100%的肯定能支持python 2.2和2.3。
Information about how to identify thestatic and dynamic builds of Boost.Python:
有關如何確定Boost.Python的靜態版和動態版:
在大多數*nix平臺上,在動態裝載的對象之間,由于符號共享方式的關系,用不同編譯工具構建的擴展模塊,當加載到同一Python實例時,我不能肯定,是否總是使用不同的Boost.Python庫。如果編譯器都具有兼容的ABI,那么,使用相同的庫將是一件好事,因為用兩個庫構建的兩個擴展模塊將可以互操作。 否則將帶來災難,因為擴展模塊和Boost.Python庫,將對如類布局之類的事情,有不同的想法。如果有人做實驗,以了解會發生什么,我將不勝感激。
configure會覆蓋主目錄下現有的user-config.jam(如果存在舊版本,會先備份它)。
注意,特性<target-os>cygwin有別于gcc工具集的子特性<flavor>cygwin,如果你同時也安裝了MinGW GCC,你可能需要明確處理這兩個特性。
Windows users, your home directory can befound by typing:
ECHO %HOMEDRIVE%%HOMEPATH%
into a command prompt window.
Windows用戶可以在命令提示符窗口中,鍵入以下命令顯示自己的主目錄:
在UNIX和類似平臺上,在配置Python構建時添加--with-pydebug,就可以構建調試版Python及相關庫。在Windows上,調試版本的Python,是在Visual Studio項目中,由“Win32 Debug”目標產生,該項目文件位于Python源代碼發行目錄的PCBuild子目錄。你也可以找到
Powered by: C++博客 Copyright © 金慶