<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ### 導航 - [索引](../genindex.xhtml "總目錄") - [模塊](../py-modindex.xhtml "Python 模塊索引") | - [下一頁](windows.xhtml "5. 在Windows平臺編譯C和C++擴展") | - [上一頁](newtypes.xhtml "3. 定義擴展類型:已分類主題") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) ? - zh\_CN 3.7.3 [文檔](../index.xhtml) ? - [擴展和嵌入 Python 解釋器](index.xhtml) ? - $('.inline-search').show(0); | # 4. 構建C/C++擴展 一個CPython的C擴展是一個共享庫(例如一個Linux上的 `.so` ,或者Windows上的 `.pyd` ),其會導出一個 *初始化函數* 。 為了可導入,共享庫必須在 [`PYTHONPATH`](../using/cmdline.xhtml#envvar-PYTHONPATH) 中有效,且必須命名遵循模塊名字,通過適當的擴展。當使用distutils時,會自動生成正確的文件名。 初始化函數的聲明如下: [PyObject](../c-api/structures.xhtml#c.PyObject "PyObject")\* `PyInit_modulename`(void)該函數返回完整初始化過的模塊,或一個 [`PyModuleDef`](../c-api/module.xhtml#c.PyModuleDef "PyModuleDef") 實例。查看 [Initializing C modules](../c-api/module.xhtml#initializing-modules) 了解更多細節。 對于僅有ASCII編碼的模塊名,函數必須是 `PyInit_<modulename>` ,將 `<modulename>` 替換為模塊的名字。當使用 [Multi-phase initialization](../c-api/module.xhtml#multi-phase-initialization) 時,允許使用非ASCII編碼的模塊名。此時初始化函數的名字是 `PyInitU_<modulename>` ,而 `<modulename>` 需要用Python的 *punycode* 編碼,連字號需替換為下劃線。在Python里: ``` def initfunc_name(name): try: suffix = b'_' + name.encode('ascii') except UnicodeEncodeError: suffix = b'U_' + name.encode('punycode').replace(b'-', b'_') return b'PyInit' + suffix ``` 可以在一個動態庫里導出多個模塊,通過定義多個初始化函數。而導入他們需要符號鏈接或自定義導入器,因為缺省時只有對應了文件名的函數才會被發現。查看 *"一個庫里的多模塊"* 章節,在 [**PEP 489**](https://www.python.org/dev/peps/pep-0489) \[https://www.python.org/dev/peps/pep-0489\] 了解更多細節。 ## 4.1. 使用distutils構建C和C++擴展 擴展模塊可以用distutils來構建,這是Python自帶的。distutils也支持創建二進制包,用戶無需編譯器而distutils就能安裝擴展。 一個distutils包包含了一個驅動腳本 `setup.py` 。這是個純Python文件,大多數時候也很簡單,看起來如下: ``` from distutils.core import setup, Extension module1 = Extension('demo', sources = ['demo.c']) setup (name = 'PackageName', version = '1.0', description = 'This is a demo package', ext_modules = [module1]) ``` 通過文件 `setup.py` ,和文件 `demo.c` ,運行如下 ``` python setup.py build ``` 這會編譯 `demo.c` ,然后產生一個擴展模塊叫做 `demo` 在目錄 `build` 里。依賴于系統,模塊文件會放在某個子目錄形如 `build/lib.system` ,名字可能是 `demo.so` 或 `demo.pyd` 。 在文件 `setup.py` 里,所有動作的入口通過 `setup` 函數。該函數可以接受可變數量個關鍵字參數,上面的例子只使用了一個子集。特別需要注意的例子指定了構建包的元信息,以及指定了包內容。通常一個包會包括多個模塊,就像Python的源碼模塊、文檔、子包等。請參數distutils的文檔,在 [分發 Python 模塊(遺留版本)](../distutils/index.xhtml#distutils-index) 來了解更多distutils的特性;本章節只解釋構建擴展模塊的部分。 通常預計算參數給 `setup()` ,想要更好的結構化驅動腳本。有如如上例子函數 [`setup()`](../distutils/apiref.xhtml#distutils.core.setup "distutils.core.setup") 的 `ext_modules` 參數是一列擴展模塊,每個是一個 `Extension` 類的實例。例子中的實例定義了擴展命名為 `demo` ,從單一源碼文件構建 `demo.c` 。 更多時候,構建一個擴展會復雜的多,需要額外的預處理器定義和庫。如下例子展示了這些。 ``` from distutils.core import setup, Extension module1 = Extension('demo', define_macros = [('MAJOR_VERSION', '1'), ('MINOR_VERSION', '0')], include_dirs = ['/usr/local/include'], libraries = ['tcl83'], library_dirs = ['/usr/local/lib'], sources = ['demo.c']) setup (name = 'PackageName', version = '1.0', description = 'This is a demo package', author = 'Martin v. Loewis', author_email = 'martin@v.loewis.de', url = 'https://docs.python.org/extending/building', long_description = ''' This is really just a demo package. ''', ext_modules = [module1]) ``` 例子中函數 [`setup()`](../distutils/apiref.xhtml#distutils.core.setup "distutils.core.setup") 在調用時額外傳遞了元信息,是推薦發布包構建時的內容。對于這個擴展,其指定了預處理器定義,include目錄,庫目錄,庫。依賴于編譯器,distutils還會用其他方式傳遞信息給編譯器。例如在Unix上,結果是如下編譯命令 ``` gcc -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -fPIC -DMAJOR_VERSION=1 -DMINOR_VERSION=0 -I/usr/local/include -I/usr/local/include/python2.2 -c demo.c -o build/temp.linux-i686-2.2/demo.o gcc -shared build/temp.linux-i686-2.2/demo.o -L/usr/local/lib -ltcl83 -o build/lib.linux-i686-2.2/demo.so ``` 這些行代碼僅用于展示目的;distutils用戶應該相信distutils能正確調用。 ## 4.2. 發布你的擴展模塊 當一個擴展已經成功的構建過,有三種方式使用。 最終用戶通常想要安裝模塊,可以這么運行 ``` python setup.py install ``` 模塊維護者應該制作源碼包;要實現可以運行 ``` python setup.py sdist ``` 有些情況下,需要在源碼發布包里包含額外的文件;這通過 `MANIFEST.in` 文件實現,查看 [Specifying the files to distribute](../distutils/sourcedist.xhtml#manifest) 了解細節。 如果源碼發行包成功構建了,維護者也可以創建二進制發行包。依賴于平臺,一個可用的命令如下 ``` python setup.py bdist_wininst python setup.py bdist_rpm python setup.py bdist_dumb ``` ### 導航 - [索引](../genindex.xhtml "總目錄") - [模塊](../py-modindex.xhtml "Python 模塊索引") | - [下一頁](windows.xhtml "5. 在Windows平臺編譯C和C++擴展") | - [上一頁](newtypes.xhtml "3. 定義擴展類型:已分類主題") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) ? - zh\_CN 3.7.3 [文檔](../index.xhtml) ? - [擴展和嵌入 Python 解釋器](index.xhtml) ? - $('.inline-search').show(0); | ? [版權所有](../copyright.xhtml) 2001-2019, Python Software Foundation. Python 軟件基金會是一個非盈利組織。 [請捐助。](https://www.python.org/psf/donations/) 最后更新于 5月 21, 2019. [發現了問題](../bugs.xhtml)? 使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 創建。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看