[cython-users] Multiple definitions of the init function ?

1,944 views
Skip to first unread message

Lionel Data

unread,
Apr 29, 2010, 11:55:04 AM4/29/10
to cython...@googlegroups.com
Hi,

I recently pulled the cython-dev repos to enjoy the c++ wrapping and it seems I can't compile several .pyx files anymore. Here is my setup.py:

ext_modules=[
    Extension("myModule",
              sources=['src/MyFile1.pyx',
                       'src/MyFile2.pyx'],
              language='c++',
              libraries=["MyLib"],
              include_dirs=['./include'],
              library_dirs=['./lib'])
    ]

setup(
  name = "myModule",
  cmdclass = {"build_ext": build_ext},
  ext_modules = ext_modules
)

When I run this script, i get those errors:

/path/to/wrapper/myModule/src/MyFile2.cpp:762: multiple definition of `initmyModule'
build/temp.linux-i686-2.6/src/MyFile2.o:/path/to/wrapper/myModule/src/MyFile1.cpp:980: first defined here
build/temp.linux-i686-2.6/src/MyFile2.o: In function `__pyx_pf_12myModule_12myClass_setPosition':
/path/to/wrapper/myModule//src/MyFile2.cpp:461: multiple definition of `__pyx_module_is_main_myModule'
build/temp.linux-i686-2.6/src/MyFile2.o:/path/to/wrapper/myModule/src/MyFile1.cpp:636: first defined here
collect2: ld returned 1 exit status

I can't find a workaround this... Any idea what I'm doing wrong ?

Dag Sverre Seljebotn

unread,
Apr 29, 2010, 12:36:30 PM4/29/10
to cython...@googlegroups.com
Lionel Data wrote:
> Hi,
>
> I recently pulled the cython-dev repos to enjoy the c++ wrapping and it
> seems I can't compile several .pyx files anymore. Here is my setup.py:
>
> ext_modules=[
> Extension("myModule",
> sources=['src/MyFile1.pyx',
> 'src/MyFile2.pyx'],

You can't have two pyx sources for the same extension module. This
should never have worked. Please create two different Extension objects.

Dag Sverre

> language='c++',
> libraries=["MyLib"],
> include_dirs=['./include'],
> library_dirs=['./lib'])
> ]
>
> setup(
> name = "myModule",
> cmdclass = {"build_ext": build_ext},
> ext_modules = ext_modules
> )
>
> When I run this script, i get those errors:
>
> /path/to/wrapper/myModule/src/MyFile2.cpp:762: multiple definition of
> `initmyModule'
> build/temp.linux-i686-2.6/src/MyFile2.o:/path/to/wrapper/myModule/src/MyFile1.cpp:980:
> first defined here
> build/temp.linux-i686-2.6/src/MyFile2.o: In function
> `__pyx_pf_12myModule_12myClass_setPosition':
> /path/to/wrapper/myModule//src/MyFile2.cpp:461: multiple definition of
> `__pyx_module_is_main_myModule'
> build/temp.linux-i686-2.6/src/MyFile2.o:/path/to/wrapper/myModule/src/MyFile1.cpp:636:
> first defined here
> collect2: ld returned 1 exit status
>
> I can't find a workaround this... Any idea what I'm doing wrong ?


--
Dag Sverre

Lisandro Dalcin

unread,
Apr 29, 2010, 12:35:08 PM4/29/10
to cython...@googlegroups.com
On 29 April 2010 12:55, Lionel Data <lione...@gmail.com> wrote:
>
> 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'], ...)




--
Lisandro Dalcin
---------------
CIMEC (INTEC/CONICET-UNL)
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
Tel: +54-342-4511594 (ext 1011)
Tel/Fax: +54-342-4511169

Lionel Data

unread,
Apr 30, 2010, 3:20:25 AM4/30/10
to cython...@googlegroups.com


2010/4/29 Lisandro Dalcin <dal...@gmail.com>

Oh, looks like  I missed that part. Thanks a lot!

Lionel

Jingpeng Wu

unread,
May 13, 2015, 1:08:05 AM5/13/15
to cython...@googlegroups.com
I have a similar error message, but probably not the same problem. I only have one .pyx file. I have searched in the internet for a while and found that it may be caused by namespace problem. but I do not know how to deal with it. anyone have a clue?

setup.py

from distutils.core import setup, Extension
from Cython.Build import cythonize
from Cython.Distutils import build_ext

setup
(ext_modules = cythonize(Extension(
           
"pythonznn",                                # the extesion name
           
# the Cython source and additional C++ source files
           sources
=["pyznn.pyx", "znn.cxx"],
           language
="c++",
           
# generate and compile C++ code
           include_dirs
=["../", "../../src", "../../zi", "/usr/people/jingpeng/libs/boost/include/"],
           libraries
=["stdc++", "fftw3", "pthread", "rt", "fftw3_threads", "boost_program_options", "boost_regex", "boost_filesystem", "boost_system", "boost_timer"],
           extra_compile_args
=['-g'],
           extra_link_args
=['-g', "-L../../ -L/usr/people/jingpeng/libs/boost/lib64/"]
     
)))


pyx file
import numpy as np
cimport numpy
as np


cdef
extern from "znn.cxx":
   
void znn_forward(   double* input_py,  unsigned int iz, unsigned int iy, unsigned int ix,\
                       
double* output_py, unsigned int oz, unsigned int oy, unsigned int ox)

def run_forward(np.ndarray[double, ndim=3, mode="c"] input  not None,\
                np
.ndarray[double, ndim=3, mode="c"] output not None):
   
"""
    run the forward pass of znn
    """

    cdef
int iz = input.shape[0]
    cdef
int iy = input.shape[1]
    cdef
int ix = input.shape[2]
    cdef
int oz = output.shape[0]
    cdef
int oy = output.shape[1]
    cdef
int ox = output.shape[2]
   
# cdef string config_fpath = "forward.config"
    znn_forward
(&input[0,0,0],  iz, iy, ix,
               
&output[0,0,0], oz, oy, ox )


znn.cxx
#ifndef ZNN_CXX_INCLUDED
#define ZNN_CXX_INCLUDED

#include "../core/network.hpp"
#include "../front_end/options.hpp"
#include <zi/zargs/zargs.hpp>

namespace z  = zi::znn;
using namespace zi::znn;

ZiARG_string(options, "", "Option file path");
ZiARG_bool(test_only, true, "Test only");

void znn_forward(   double* input_py,  unsigned int iz, unsigned int iy, unsigned int ix,
                   
double* output_py, unsigned int oz, unsigned int oy, unsigned int ox)
{
   
// options, create fake main function parameters
   
char *argv[] = { "run_znn_forward", "--options=forward.config", NULL };
   
int argc=2;
    zi
::parse_arguments(argc, argv);
    options_ptr op
= options_ptr(new options(ZiARG_options));
    op
->save();
   
// create network
    network net
(op);

   
// initialization
    double3d_ptr pinput
= volume_pool.get_double3d(ix,iy,iz);
    double3d
& input = *pinput;
   
// input.data() = input_array.get_data();
   
int index = 0;
   
for (std::size_t z=0; z<iz; z++)
       
for (std::size_t y=0; y<iy; y++)
           
for (std::size_t x=0; x<ix; x++)
           
{
                input
[x][y][z] = input_py[index];
                index
++;
           
}
    std
::list<double3d_ptr> pinputs;
    pinputs
.push_back(pinput);

   
// prepare output
    std
::list<double3d_ptr> poutputs;
    poutputs
= net.run_forward(pinputs);
    double3d
& output = *(poutputs.front());

   
// give value to python numpy output array
    index
= 0;
   
for (std::size_t z=0; z<oz; z++)
       
for (std::size_t y=0; y<oy; y++)
           
for (std::size_t x=0; x<ox; x++)
           
{
                output_py
[index] = output[x][y][z];
                index
++;
           
}
}

#endif // ZNN_CXX_INCLUDED

Pab Peter

unread,
Jun 2, 2016, 1:39:48 AM6/2/16
to cython-users
Thanks! This works for me.
Reply all
Reply to author
Forward
0 new messages