Cython和distutils

Rue*_*eck 4 python distutils cython

我想使用Cython将多个.pyx文件转换为可执行包(.DLL).

如何通过distutils从多个.pyx创建单个Windows DLL?

使用的样本:

sub1.pyx:

cimport sub1

class A():
    def test(self, val):
        print "A", val
Run Code Online (Sandbox Code Playgroud)

sub1.pxd:

cdef class A:
    cpdef test(self,val)
Run Code Online (Sandbox Code Playgroud)

sub2.pyx:

cimport sub2

class B():
    def test(self):
        return 5
Run Code Online (Sandbox Code Playgroud)

sub2.pxd:

cdef class B:
    cpdef test(self)
Run Code Online (Sandbox Code Playgroud)

init .py:

cimport sub1
cimport sub2

import sub1
import sub2
Run Code Online (Sandbox Code Playgroud)

setup.py:

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

ext_modules = [Extension("sub", ["__init__.pyx", "sub1.pyx", "sub2.pyx"])]

setup(
  name = 'Hello world app',
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules
)
Run Code Online (Sandbox Code Playgroud)

错误:

sub1.obj : error LNK2005: ___pyx_module_is_main_sub already defined in __init__.obj
sub1.obj : error LNK2005: _initsub already defined in __init__.obj
sub2.obj : error LNK2005: ___pyx_module_is_main_sub already defined in __init__.obj
sub2.obj : error LNK2005: _initsub already defined in __init__.obj
Creating library build\temp.win32-2.7\Release\sub.lib and object build\temp.win32-2.7\Release\sub.exp
C:\temp\ctest\sub\sub.pyd : fatal error LNK1169: one or more multiply defined symbols found
Run Code Online (Sandbox Code Playgroud)

J_Z*_*Zar 8

我没有意识到这一点:

http://groups.google.com/group/cython-users/browse_thread/thread/cbacb7e848aeec31

我报告了cython的一个主要编码员(Lisandro Dalcin)的答案(抱歉交叉发布):

ext_modules=[ 
    Extension("myModule", 
              sources=['src/MyFile1.pyx', 
                       'src/MyFile2.pyx'], 
Run Code Online (Sandbox Code Playgroud)

你不能拥有一个由两个不同来源构建的"myModule".也许你可以添加一个"src/myModule.pyx"文件,下面两行:

# file: myModule.pyx 
include "MyFile1.pyx" 
include "MyFile2.pyx" 
Run Code Online (Sandbox Code Playgroud)

然后使用

Extension("myModule", sources=['src/myModule.pyx'], ...) 
Run Code Online (Sandbox Code Playgroud)