
正文
自发行python版本制作(二)编译
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
本系列文章第二篇主要说明windows环境的编译环境搭建以及编译过程。
编译环境选择:
1.选用作神一样存在的Microsoft Visual C++ Compiler for Python 2.7为编译器使用。不选用vs的原因在于vs过于庞大不太适合我这种喜欢小而全的思想。这里也没有选择Announcing Visual C++ Build Tools 2015作为编译环境因为这个东东没有离线安装包,在很多地方没有离线安装方式就可以说根本无法使用。
2.使用vcbuild方式进行编译。这里没有选择msbuild进行编译是因为神一样存在的Microsoft Visual C++ Compiler for Python 2.7是vs2008的精简版,在2008中还没有讲vcbuild换成msbuild方式。这两种方式都是比较不错的方式,只不过上面没有选用vs2015的精简版(Announcing Visual C++ Build Tools 2015)所以无法选用msbuil而已。
3.选用命令行方式进行编译,也算是一个挑战。因为在大家的概念中在windows上编译的东东都必须使用vs的界面进行,网络上这方面的资料也比较少。所以,这里当作一个挑战进行。不过在使用命令行编译的过程中发现vs的整套编译环境比较全面而且灵活性很高,这里给vs命令行编译过程点个赞。
环境搭建:
0.操作系统为win10 64位系统,python源码版本选用当前最新版本2.7.11。
1.下载并安装神一样存在的Microsoft Visual C++ Compiler for Python 2.7,添加INCLUDE和LIBPATH到环境变量中。
2.下载并安装Microsoft Visual C++ 2008 Redistributable Package (x64)和Microsoft Visual C++ 2008 Redistributable Package (x86)。
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从中可以看到
Quick Start Guide
----------------- . Install Microsoft Visual Studio , any edition.
. Install Microsoft Visual Studio , any edition, or Windows SDK 7.1
and any version of Microsoft Visual Studio newer than .
. Install Subversion, and make sure 'svn.exe' is on your PATH.
. Run "build.bat -e" to build Python in -bit Release configuration.
. (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./ are
specific to Visual Studio , 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./
Visual Studio (7.1)
PC/VS8./
Visual Studio (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. (default, Feb , ::) [MSC v. 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






