自發行python版本製做(二)編譯

本系列文章第二篇主要說明windows環境的編譯環境搭建以及編譯過程。html

 

編譯環境選擇:python

1.選用做神同樣存在的Microsoft Visual C++ Compiler for Python 2.7爲編譯器使用。不選用vs的緣由在於vs過於龐大不太適合我這種喜歡小而全的思想。這裏也沒有選擇Announcing Visual C++ Build Tools 2015做爲編譯環境由於這個東東沒有離線安裝包,在不少地方沒有離線安裝方式就能夠說根本沒法使用。sql

2.使用vcbuild方式進行編譯。這裏沒有選擇msbuild進行編譯是由於神同樣存在的Microsoft Visual C++ Compiler for Python 2.7是vs2008的精簡版,在2008中尚未講vcbuild換成msbuild方式。這兩種方式都是比較不錯的方式,只不過上面沒有選用vs2015的精簡版(Announcing Visual C++ Build Tools 2015)因此沒法選用msbuil而已。windows

3.選用命令行方式進行編譯,也算是一個挑戰。由於在你們的概念中在windows上編譯的東東都必須使用vs的界面進行,網絡上這方面的資料也比較少。因此,這裏看成一個挑戰進行。不過在使用命令行編譯的過程當中發現vs的整套編譯環境比較全面並且靈活性很高,這裏給vs命令行編譯過程點個贊。api

 

環境搭建:網絡

0.操做系統爲win10 64位系統,python源碼版本選用當前最新版本2.7.11。socket

1.下載並安裝神同樣存在的Microsoft Visual C++ Compiler for Python 2.7,添加INCLUDE和LIBPATH到環境變量中。ide

2.下載並安裝Microsoft Visual C++ 2008 Redistributable Package (x64)Microsoft Visual C++ 2008 Redistributable Package (x86)svn

3.下載並安裝ActivePerl,並檢查環境變量是否生效。函數

4.下載並安裝NASM

5.下載SQLite 3.6.21,並將sqlite源代碼解壓到python源代碼目錄下的externals目錄下。

6.下載bzip2-1.0.6.tar.gz。並將bz源代碼解壓到python源代碼目錄下的externals目錄下。

7.下載openssl-1.0.2d.tar.gz。並將openssl源代碼解壓到python源代碼目錄下的externals目錄下。

其上,每個東東意義分別爲:1,2爲基礎編譯環境。3,4爲openssl在windows上編譯所必須的。5,6,7爲python的相關開源項目。

 

編譯過程:

0.先開始,能夠從pcbuild目錄下找到readme.txt從中能夠看到

1 Quick Start Guide
2 -----------------
3 
4 1.  Install Microsoft Visual Studio 2008, any edition.
5 2.  Install Microsoft Visual Studio 2010, any edition, or Windows SDK 7.1
6     and any version of Microsoft Visual Studio newer than 2010.
7 3.  Install Subversion, and make sure 'svn.exe' is on your PATH.
8 4.  Run "build.bat -e" to build Python in 32-bit Release configuration.
9 5.  (Optional, but recommended) Run the test suite with "rt.bat -q".

能夠看到最新的python源代碼支持vs2010進行編譯。另外還能看到:

Legacy support
--------------

You can find build directories for older versions of Visual Studio and
Visual C++ in the PC directory.  The project files in PC/VS9.0/ are
specific to Visual Studio 2008, and will be fully supported for the life
of Python 2.7.

The following legacy build directories are no longer maintained and may
not work out of the box.

PC/VC6/
    Visual C++ 6.0
PC/VS7.1/
    Visual Studio 2003 (7.1)
PC/VS8.0/
    Visual Studio 2005 (8.0)

這裏能夠看到vs2008是一個如今還支持,可是在pc/vs9.0目錄下。

2.到pc/readme.txt中能夠獲得編譯過程基本是從build.bat開始的。因此到pc/vs9.0下直接查找build.bat。打開build.bat能夠看到這是一個比較簡單的腳本,按照參數進行填寫參數,調用build.bat基本就能夠了。

3.代碼Visual C++ 2008 64-bit Command Prompt,切換到python代碼目錄的pc/vs9.0下。執行

build.bat -c Release -p x64 -t Rebuild

直接報錯,提示找不到cl命令,和目標是64位,編譯出來是32位。解決辦法:

build.bat -c Release -p x64 -t Rebuild /useenv /logcommands /platform:x64

添加了後面的三個參數,這三個參數是vcbuid的參數,參數意義詳見:VCBUILD 選項

最終build.bat腳本解釋出:

vcbuild "D:\\Python-2.7.11\PC\VS9.0\pcbuild.sln" /rebuild "Release|x64" /useenv /logcommands /platform:x64

4.刪除咱們不須要的子項目:

從pcbuild.sln中能夠能夠看到有_ctypes,_ctypes_test,_elementtree,_hashlib,_msi,_multiprocessing,_socket,_testcapi,pyexpat,select,unicodedata,winsound,_bsddb,_bz2,_ssl,_sqlite3,_tkinter,w9xpopen

在上篇文章中說到需求不使用tk講tk刪除。bsddb比較古老的db標準,如今估計沒人使用也刪了。_ssl,_hashlib都是用openssl,在windows編譯openssl比較麻煩暫時刪除,以後搞定openssl的windows編譯以後再添加進來。w9xpopen是win9X的環境支持,也刪掉。

5.使用上面的編譯命令進行編譯:

發現仍有編譯不過問題,提示編譯dll時找不到入口函數。調查後發現連接dll時缺乏參數/dll,詳情參見:Linker Options

根據這個調查發現vcproj中的Configuration中配置的ConfigurationTypes Enumeration正確。可是依然不添加/dll參數,查找VCLinkerTool的標籤屬性發現缺乏LinkDLL參數,添加後編譯經常。

6.編譯結果:

編譯出python.exe,pythonw.exe,python27.dll等等,雙擊python.exe:

Python 2.7.11 (default, Feb  9 2016, 10:51:22) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.>>> import os
>>> print os
<module 'os' from 'D:\\Python-2.7.11\lib\os.pyc'>
>>>

能夠執行。下一篇文章會詳細介紹編譯目錄下編譯出的文件的意義,以及進一步的添加win api相關。

 

參考文獻:

https://docs.python.org/2/using/windows.html

 

Building C++ Projects in Visual Studio

https://msdn.microsoft.com/en-us/library/7s88b19e(v=vs.90).aspx


C/C++ Building Reference

https://msdn.microsoft.com/en-us/library/91621w01.aspx


Solutions and Projects in Visual Studio

https://msdn.microsoft.com/en-us/library/b142f8e7.aspx


Solution (.Sln) File

https://msdn.microsoft.com/en-us/library/bb165951(v=VS.90).aspx


Extending Solution Explorer

https://msdn.microsoft.com/en-us/library/bb166499(v=vs.90).aspx

 

Property Inheritance

https://msdn.microsoft.com/en-us/library/tybz7dex(v=vs.90).aspx

 

Project Files

https://msdn.microsoft.com/en-us/library/2208a1f2(v=vs.90).aspx


關於VCPROJ文件的說明

http://blog.sina.com.cn/s/blog_6288219501011nib.html

 

VCProject Properties

https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.vcprojectengine.vcproject_properties(v=vs.90).aspx


Visual C++ Project Model

https://msdn.microsoft.com/en-us/library/2eydyk57(v=vs.90).aspx


Visual C++ Code Model

https://msdn.microsoft.com/en-us/library/t41260xs(v=vs.90).aspx


Microsoft.VisualStudio.VCProjectEngine Namespace

https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.vcprojectengine(v=vs.90).aspx


ConfigurationTypes Enumeration

https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.vcprojectengine.configurationtypes(v=vs.90).aspx


VCLinkerTool Members

https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.vcprojectengine.vclinkertool_members(v=vs.90).aspx

 

VCBUILD 選項

https://msdn.microsoft.com/zh-cn/library/cz553aa1(VS.80).aspx


Linker Options

https://msdn.microsoft.com/en-us/library/y0zzbyt4(v=vs.90).aspx

 

RegOpenKey function

https://msdn.microsoft.com/en-us/library/windows/desktop/ms724895(v=vs.85).aspx

相關文章
相關標籤/搜索