用disutils建立C和C++扩展

以下为源网址翻译,翻译的不好还请见谅。

源网址:https://docs.python.org/2/extending/building.html#building

3. Building C and C++ Extensions with distutils
3. 用disutils建立C和C++扩展
Starting in Python 1.4, Python provides, on Unix, a special make file for building make files for building dynamically-linked extensions and custom interpreters. 
Starting with Python 2.0, this mechanism (known as related to Makefile.pre.in, and Setup files) is no longer supported. 
Building custom interpreters was rarely used, and extension modules can be built using distutils.
从Python1.4开始,它在Unix上提供了一种为建立动态链接扩展和传统解释器的特殊文件生成方式。
从Python2.0开始,这种机制(已知的,预先生成载入文件和Setup文件)就不再支持了。
传统解释器的简历也很少被使用,并且使用distutils扩展模块被建立。

Building an extension module using distutils requires that distutils is installed on the build machine, which is included in Python 2.x and available separately for Python 1.5. 
Since distutils also supports creation of binary packages, users don’t necessarily need a compiler and distutils to install the extension.
使用disutils模块简历扩展模块的方式要求在构筑机器上安装distutils(在Python2.X或者个别Python1.5上被安装)
由于distutils同样也支持二进制包的创建,使用者不需要用编译器和distutils去安装扩展包

A distutils package contains a driver script, setup.py. This is a plain Python file, which, in the most simple case, could look like this:
一个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])
 With this setup.py, and a file demo.c, running
用这个setup.py,和一个demo.c,运行

python setup.py build
 will compile demo.c, and produce an extension module named demo in the build directory. 
Depending on the system, the module file will end up in a subdirectory build/lib.system, and may have a name like demo.so or demo.pyd.
将会编译demo.c,并且在build目录下产生一个扩展模块名叫demo。
根据不同系统,这个模块文件将在目录build/lib.system结束,并且有一个名字叫做demo.so 或者demo.pyd。

 In the setup.py, all execution is performed by calling the setup function. 
This takes a variable number of keyword arguments, of which the example above uses only a subset. 
Specifically, the example specifies meta-information to build packages, and it specifies the contents of the package. 
Normally, a package will contain of addition modules, like Python source modules, documentation, subpackages, etc. 
Please refer to the distutils documentation in Distributing Python Modules (Legacy version) to learn more about the features of distutils; this section explains building extension modules only.
在setup.py文件里,所有的执行将调用setup函数。
这将采用一系列不同的关键字参数,上面的实例只使用了一部分。
特殊地,示例指定元信息构建包,并且它指定了包的内容。
正常地,一个包将包含额外的模块,像Python源模块,文档,分包等等
请参考disutils文件中的Distributing Python Modules (Legacy version)去学习更多的disutils特征;这个部分只解释了构建扩展模块。

It is common to pre-compute arguments to setup(), to better structure the driver script. 
In the example above, the ext_modules argument to setup() is a list of extension modules, each of which is an instance of the Extension. 
In the example, the instance defines an extension named demo which is build by compiling a single source file, demo.c.
预先计算构建参数能够更好地构筑驱动脚本。
在上面的例子中,“ext_modules”构建参数是一个扩展模块的列表,列表中的每一个是一个扩展的实例。
在上面的例子中,实例定义了一个扩展了demo.c文件的demo扩展包。

  In many cases, building an extension is more complex, since additional preprocessor defines and libraries may be needed. 
This is demonstrated in the example below.
在许多种情况下,构建一个扩展将更负载,因为这可能需要扩展的预处理器定义和函数库。
在下面的示例中演示。

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])
 In this example, setup() is called with additional meta-information, which is recommended when distribution packages have to be built. 
For the extension itself, it specifies preprocessor defines, include directories, library directories, and libraries. 
Depending on the compiler, distutils passes this information in different ways to the compiler. 
For example, on Unix, this may result in the compilation commands
在这个例子中,setup()包含了额外的元信息,这些将在分包中被构建。
在它扩展的时候,它指定预处理其的定义,包括路径、二进制路径和函数库。
依据编译器,disutils用不同方式将这些信息传给编译器。
举例,在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
 These lines are for demonstration purposes only; distutils users should trust that distutils gets the invocations right.
这几行只示范目的,distutils使用者应该相信distutils会被正确调用。

3.1. Distributing your extension modules
部署你的扩展模块

When an extension has been successfully build, there are three ways to use it.
当一个扩展模块已被成功构筑,这里有三种方式使用它。

End-users will typically want to install the module, they do so by running
终端用户将更想要去安装这模块,他们通过运行以下代码来实现
 
python setup.py install
 Module maintainers should produce source packages; to do so, they run
模块维护这将产生源包,他们通过运行以下代码来实现

python setup.py sdist

 In some cases, additional files need to be included in a source distribution; this is done through a MANIFEST.in file; see the distutils documentation for details.
在有些情况下,额外的文件需要包含在源部署文件中;这些将通过MANIFEST.in文件完成;详细看distutils文档

If the source distribution has been build successfully, maintainers can also create binary distributions. 
Depending on the platform, one of the following commands can be used to do so.
如果源部署文档已经成功构筑,维护这将会创建二进制部署文件。

依靠平台,下面代码之一将经常用来这么做。


python setup.py bdist_wininst
python setup.py bdist_rpm
python setup.py bdist_dumb

附:扩展阅读
(1)Distutils是什么?
   Distutils可以用来在Python环境中构建和安装额外的模块。目前为止,Distutils只能处理Python的C/C++库。
   Distutils术语包括模块发布、纯模块发布、非纯模块发布和发布根。setup脚本是使用Distutils构建、发布和安装模块的核心
   setup脚本作用是向Disutils描述描述发布的信息。具体例子看参考网址2。
参阅博客网址:
1.http://blog.csdn.net/gqtcgq/article/details/49365979 - Python深入:Distutils安装包
2.http://blog.csdn.net/gqtcgq/article/details/49255995 - Python深入:Distutils发布Python模块


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值