Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Windows上构建64位C Python扩展_Python_C_64 Bit - Fatal编程技术网

在Windows上构建64位C Python扩展

在Windows上构建64位C Python扩展,python,c,64-bit,Python,C,64 Bit,我问这个问题是因为我需要构建一个特定的模块(aspell_python),以使用在Windows7(当然是64位)机器上运行的64位python 2.6。我还一直想知道如何使用C代码加速某些函数,因此我希望将来为Python制作自己的外部C模块 有谁能告诉我在C中成功构建64位Python扩展所需的步骤吗?我知道Python,我知道C,但我不知道VisualStudio或Windows特定开发人员的事情。我试图遵循Python网站上的官方指南(http://docs.python.org/ext

我问这个问题是因为我需要构建一个特定的模块(aspell_python),以使用在Windows7(当然是64位)机器上运行的64位python 2.6。我还一直想知道如何使用C代码加速某些函数,因此我希望将来为Python制作自己的外部C模块


有谁能告诉我在C中成功构建64位Python扩展所需的步骤吗?我知道Python,我知道C,但我不知道VisualStudio或Windows特定开发人员的事情。我试图遵循Python网站上的官方指南(http://docs.python.org/extending/windows.html#building-在windows上)使用Visual Studio 2008(这是此处唯一可用的商业产品)但是,即使是最基本的示例也无法生成。

之前,我已经在64位Windows上成功编译了Python的C扩展,方法是从扩展源发行版的顶级目录中的“Visual Studio 2008 x64 Win64命令提示符”运行以下命令:

set DISTUTILS_USE_SDK=1
set MSSdk=1
python setup.py install
我会使用:只需下载、解压缩、运行init bat,然后

如果这不起作用,并且您可以让Microsoft的C编译器环境工作,请尝试。将普通Python扩展与其生成的C版本进行比较。最新摘录:

c_prime.pyx:

def calculate(long limit):
    cdef long current
    cdef long divisor
    primes = []
    divisor = 0
    for current in range(limit):
        previous = []
        for divisor in range(2, current):
            if current % divisor == 0:
                break
        if divisor == current - 1:
            primes.append(current)
    return primes
setup.py:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(
  name = 'PrimeTest',
  ext_modules=[
    Extension('c_prime', ['c_prime.pyx'])
    ],
  cmdclass = {'build_ext': build_ext}
)
汇编:

测试_prime.py:

from timeit import Timer

t = Timer('c_prime.calculate(10000)', 'import c_prime')
reps = 5
print(sum(t.repeat(repeat=reps, number=1)) / reps)

添加即使是最基本的示例也是失败的。例如:在遵循每个方向之后,当我尝试构建示例时,我得到链接器错误,它找不到python26.lib。然后,我尝试使用随我的python版本一起分发的python26.lib,但只导致了两个链接器错误:1>example.obj:error LNK2019:unresolved external symbol\uu imp\uuuuuuuuuuupy\u nonestuct在函数exfoo 1>example中引用。obj:error LNK2019:unresolved external symbol\uuu imp\uuuupy\u InitModule4中引用的unresolved外部符号_initexample在那之后,我试图通过使用VS2008从源代码构建整个python 2.6项目来构建python26.lib,这当然会导致一些错误。但是,它生成了一个python26_d.lib,当使用“python setup.py build”与example_ntBuilding example_nt一起编译时,仍然会出现链接器错误如果您已经安装了带有64位编译器的Visual Studio 2008 Professional,则应在Python 2.6.6 AMD64上开箱即用。很可能您没有正确安装Visual Studio 2008 Professional。64位编译器是一个需要在安装时启用的选项。如果您使用的是VisualStudio2008Express,那么它并没有附带64位编译器。对于我来说,这似乎适用于VS2012。。。我曾经很痛苦。