4

I want to use Cython to convert multiple .pyx files into an executable package (.DLL).

How do I create a single Windows DLL from multiple .pyx via distutils?

Sample used:

sub1.pyx:

cimport sub1

class A():
    def test(self, val):
        print "A", val

sub1.pxd:

cdef class A:
    cpdef test(self,val)

sub2.pyx:

cimport sub2

class B():
    def test(self):
        return 5

sub2.pxd:

cdef class B:
    cpdef test(self)

init.py:

cimport sub1
cimport sub2

import sub1
import sub2

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
)

Error:

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
Ruediger Jungbeck
  • 2,836
  • 5
  • 36
  • 59

1 Answers1

8

I was not aware of this :

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

I report the answer of one of the main coders (Lisandro Dalcin) of cython (sorry for cross posting):

ext_modules=[ 
    Extension("myModule", 
              sources=['src/MyFile1.pyx', 
                       'src/MyFile2.pyx'], 

You cannot have a single "myModule" built from two different sources. Perhaps you could add a "src/myModule.pyx" file, with the two lines below:

# file: myModule.pyx 
include "MyFile1.pyx" 
include "MyFile2.pyx" 

and then use

Extension("myModule", sources=['src/myModule.pyx'], ...) 
SimplyKnownAsG
  • 904
  • 9
  • 26
J_Zar
  • 2,034
  • 2
  • 21
  • 34
  • Are you really giving multiple ways of solving his problem, or editing the answers you have already given? If you are editing, please clean up this post and use the "edit" link below the answer you want to keep. – Mike Pennington May 02 '12 at 10:04
  • The problem is that SO does not work correctly with Opera 11.62. I am not able to use the carriage return key to have a newline in the post edit. – J_Zar May 02 '12 at 11:41
  • But this would handle all the pyx files as one module (ie put all symbols into one namespace)? – Ruediger Jungbeck May 03 '12 at 05:32
  • 1
    Yes. The result will be a single module (pyd file). – J_Zar May 03 '12 at 06:43