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.
擴(kuò)展和內(nèi)嵌的關(guān)鍵性區(qū)別是,C++ main()函數(shù)的位置是在Python解譯器程序中,還是在其他程序中? 注意,即使是在程序中內(nèi)嵌Python,擴(kuò)展模塊往往是Python代碼利用C/C++功能最好的方式,所以,這兩種模式實際上是以擴(kuò)展模塊的使用為中心的。
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().
除了極少數(shù)情況,擴(kuò)展模塊是按具有單一入口點的動態(tài)庫構(gòu)建的,這意味著,你可以修改它們,而不必重建其它擴(kuò)展模塊或包含main()的執(zhí)行程序。
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項目,它會在需要時自動構(gòu)建那些二進(jìn)制庫。你的第一個測試,可能需要較長的時間,你需要等待Boost.Python的構(gòu)建,但這樣做,讓你不必?fù)?dān)憂錯綜復(fù)雜的構(gòu)建,如,某個特定編譯器配置該使用哪個二進(jìn)制庫,也讓你免于自己設(shè)置編譯器選項。
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.
當(dāng)然,也可以使用其他的編譯系統(tǒng),來構(gòu)建Boost.Python及其擴(kuò)展,但它們不是Boost正式支持的。所有“我無法編譯Boost.Python”的問題,99%以上是因為試圖使用另一種編譯系統(tǒng),而沒有先按這個教程做。
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.
如果你想使用另一個系統(tǒng),我們建議你先遵循這些指示,然后用以下參數(shù)調(diào)用bjam,
來輸出它對文件執(zhí)行的構(gòu)建命令,這樣你就可以看到你的編譯系統(tǒng)需要做些什么。
Get Boost; see sections 1 and 2 [Unix/Linux, Windows] of theBoost Getting Started Guide.
獲取Boost;見Boost入門指南第1和第2節(jié)[Unix/Linux,Windows]。
Get the bjam build driver. See section 5 [Unix/Linux,Windows] of the Boost Getting Started Guide.
獲取bjam構(gòu)建驅(qū)動器。見Boost入門指門第5節(jié)[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.
調(diào)用bjam。將入門指南第5節(jié)例示的調(diào)用命令中的“stage”參數(shù)替換為“test”,來構(gòu)建所有測試目標(biāo)。 同時添加參數(shù)“--verbose-test”來查看測試運行時生成的輸出。
On Windows, your bjam invocation might look something like:
在Windows上,你的bjam調(diào)用可能像這樣:
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風(fēng)格的斜杠,而不是你可能更熟悉的反斜杠。斜杠應(yīng)該到處可用,除了命令提示符窗口,在那里您應(yīng)該使用反斜杠。
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.
如果你能成功地遵循這個過程,你將構(gòu)建出一個擴(kuò)展模塊,名為extending,并運行一個名為test_extending.py的Python腳本測試它。你也將構(gòu)建并運行一個內(nèi)嵌python的簡單應(yīng)用程序,名為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安裝。你也許要在最初幾次調(diào)用bjam時,傳入--debug-configuration參數(shù),以確保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新手,此時,最好暫時不理會構(gòu)建問題,而是集中精力去學(xué)習(xí)庫,盡快學(xué)完教程,然后可能是參考文檔,同時通過修改快速入門工程,試用你所學(xué)到的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.
如果你滿足于此,擴(kuò)展模塊的源文件永遠(yuǎn)是extending.cpp,位于Boost發(fā)布目錄中,并永遠(yuǎn)按extending導(dǎo)入,那么你就可以到此為止。 不過很可能是,你會想作一些變動。有許多事你可以做,而無須深入學(xué)習(xí)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.
你剛剛構(gòu)建的工程,是由當(dāng)前目錄下的兩個文件詳細(xì)說明的:boost-build.jam(它告訴bjam,哪里可以找到Boost編譯系統(tǒng)的解釋代碼),和Jamroot(它描述了你剛剛構(gòu)建的目標(biāo))。這些文件具有詳細(xì)的注釋,所以它們應(yīng)該很容易修改。不過小心,要保留空白字符。如;這樣的標(biāo)點符號,如果周圍沒有空白字符,bjam將無法識別。
You'll probably want to copy this project elsewhere so you canchange it without modifying your Boost distribution. To do that,simply
你可能想把該工程復(fù)制到其他位置,這樣你就可以修改它,而不必在Boost發(fā)布目錄中更改。要做到這一點,只需
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:
構(gòu)建擴(kuò)展模塊或內(nèi)嵌應(yīng)用程序時,涉及的其他源文件的名字,可以在Jamroot中列出,分別列于extending.cpp或embedding.cpp旁邊。只是每個文件名周圍,務(wù)必留下空白字符:
… 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.
當(dāng)然,如果你想要更改源文件名,你可以編輯Jamroot中的文件名,來告訴Boost.Build。
The name of the extension module is determined by two things:
擴(kuò)展模塊的名字,由兩個地方?jīng)Q定:
To change the name of the extension module from extending tohello, you'd edit Jamroot, changing
要將擴(kuò)展模塊名從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二進(jìn)制庫的服務(wù)。
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.
如果你需要在系統(tǒng)上安裝一個常規(guī)的Boost.Python二進(jìn)制庫,Boost入門指南將帶你一步一步創(chuàng)建它。如果從源文件構(gòu)建二進(jìn)制,你也許要向bjam提供--with-python參數(shù)(或向configure提供--with-libraries=python參數(shù)),這樣就會僅僅構(gòu)建Boost.Python二進(jìn)制,而不是所有的Boost二進(jìn)制。
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文件,用來指定編譯系統(tǒng)可用的工具和庫。您可能需要創(chuàng)建或編輯user-config.jam,來告訴Boost.Build如何調(diào)用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操作系統(tǒng),并且你運行了Boost configure腳本,它可能已經(jīng)為你生成了一個user-config.jam4。如果你configure/make就能成功構(gòu)建Boost.Python,你的user-config.jam文件可能已經(jīng)是正確的了。
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
如果你的平臺有一個相當(dāng)“標(biāo)準(zhǔn)”的python安裝,你可能不需要對它做特別描述。如果你沒有在user-config.jam中配置python(并且沒有在Boost.Build命令行中指定--without-python),Boost.Build將自動執(zhí)行以下等效語句
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。 不過,這只會發(fā)生在當(dāng)使用Boost.Python項目文件時(例如,當(dāng)被另一個項目引用時,如快速入門所示方法)。反之,如果你鏈接單獨編譯的Boost.Python二進(jìn)制,你應(yīng)該建立一個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提供下列部分或全部可選參數(shù)。
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)將成為默認(rèn)值。如果要使用python 2.4構(gòu)建,請在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參數(shù)中提供解釋器的路徑:
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:
如果一個特定工具集使用了一個單獨構(gòu)建的Python,你可以在condition參數(shù)中提供該工具集。
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上自己構(gòu)建了python和python調(diào)試版,你可能會看到:
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:
你可以設(shè)置user-config.jam,讓W(xué)indows上的bjam能夠構(gòu)建和測試兩個版本的python擴(kuò)展,既有Windows版,也有Cygwin版。只需在condition參數(shù)中,為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
當(dāng)你在構(gòu)建請求中加入target-os=cygwin,它就應(yīng)該使用cygwin版本的python進(jìn)行構(gòu)建: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.
另一種方式應(yīng)該也行(使用Cygwin上的bjam,以windows python為目標(biāo)),但在本文寫作之時,Boost.Build的工具集對那種構(gòu)建方式的支持好像有問題。
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目前選擇不同目標(biāo)的方式下,可能你的編譯請求必須非常明確。舉例來說,如有:
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:
構(gòu)建會產(chǎn)生一個錯誤。你需要這樣寫才行:
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自動構(gòu)建和鏈接正確的庫,而是選擇使用預(yù)建的Boost.Python庫,你需要考慮一下鏈接哪個庫。Boost.Python二進(jìn)制庫同時具有靜態(tài)和動態(tài)庫。請為您的應(yīng)用小心選擇正確的庫。2
The dynamic library is the safest and most-versatile choice:
動態(tài)庫是最安全和最通用的選擇:
It might be appropriate to use the static Boost.Python library inany of the following cases:
下列任一情況下,可能適合使用靜態(tài)的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調(diào)試”,進(jìn)行構(gòu)建,它增加了額外的檢查和測試,對擴(kuò)展模塊的開發(fā)者非常有用。調(diào)試配置所使用的數(shù)據(jù)結(jié)構(gòu),包含了附加的成員,因此,用python調(diào)試模式構(gòu)建的Python可執(zhí)行程序,不能使用非調(diào)試模式編譯的擴(kuò)展模塊或庫,反之亦然。
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.
由于在大多數(shù)Python發(fā)行版中,沒有提供預(yù)建的“python調(diào)試”版的Python可執(zhí)行程序和庫7,而且我們不想強(qiáng)迫我們的用戶去構(gòu)建它們,所以,在Boost.Python的debug版中(默認(rèn)是debug版),沒有自動打開python調(diào)試模式,作為替代,有一個特別的構(gòu)建屬性,稱為python-debugging,使用該構(gòu)建屬性時,會定義正確的預(yù)處理符號,并選擇正確的庫進(jìn)行鏈接。
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平臺上,僅當(dāng)定義了Py_DEBUG符號時,才會使用調(diào)試版Python的數(shù)據(jù)結(jié)構(gòu)。在許多Windows編譯器上,當(dāng)擴(kuò)展模塊用預(yù)處理符號_DEBUG構(gòu)建時,Python默認(rèn)會強(qiáng)制鏈接一個特殊的,調(diào)試版本的Python DLL。由于這個符號很常用(甚至沒有Python時),所以,在boost/python/detail/wrap_python.hpp中包含Python.h時,Boost.Python暫時取消了_DEBUG定義——除非定義了BOOST_DEBUG_PYTHON。其結(jié)果是,如果你想要“Python調(diào)試”,并且你沒有用Boost.Build,你務(wù)必定義BOOST_DEBUG_PYTHON,不然python調(diào)試會被禁用。
To run the full test suite for Boost.Python, invoke bjam in thelibs/python/test subdirectory of your Boost distribution.
在Boost的libs/python/test子目錄下, 調(diào)用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)之前的版本,您將需要創(chuàng)建一個與MinGW兼容的Python庫;Python發(fā)行隨帶的庫只能用于微軟兼容的連接器。請轉(zhuǎn)到安裝Python模塊,“構(gòu)建擴(kuò)展:提示和技巧”一章,“非微軟”一節(jié),按照指示來創(chuàng)建libpythonXX.a,其中XX為你的Python安裝的主要和次要版本號。
注意,盡管我們在Python 2.2上測試過Boost.Python的早期版本,并且我們認(rèn)為我們沒有破壞兼容性,但本次發(fā)行的Boost.Python可能沒有在Python 2.4以前的版本上測試過,所以我們不是100%的肯定能支持python 2.2和2.3。
Information about how to identify thestatic and dynamic builds of Boost.Python:
有關(guān)如何確定Boost.Python的靜態(tài)版和動態(tài)版:
在大多數(shù)*nix平臺上,在動態(tài)裝載的對象之間,由于符號共享方式的關(guān)系,用不同編譯工具構(gòu)建的擴(kuò)展模塊,當(dāng)加載到同一Python實例時,我不能肯定,是否總是使用不同的Boost.Python庫。如果編譯器都具有兼容的ABI,那么,使用相同的庫將是一件好事,因為用兩個庫構(gòu)建的兩個擴(kuò)展模塊將可以互操作。 否則將帶來災(zāi)難,因為擴(kuò)展模塊和Boost.Python庫,將對如類布局之類的事情,有不同的想法。如果有人做實驗,以了解會發(fā)生什么,我將不勝感激。
configure會覆蓋主目錄下現(xiàn)有的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構(gòu)建時添加--with-pydebug,就可以構(gòu)建調(diào)試版Python及相關(guān)庫。在Windows上,調(diào)試版本的Python,是在Visual Studio項目中,由“Win32 Debug”目標(biāo)產(chǎn)生,該項目文件位于Python源代碼發(fā)行目錄的PCBuild子目錄。你也可以找到
Powered by: C++博客 Copyright © 金慶