0

I have:

my_volume.pxd

cdef float c_cube(float x)

my_volume.pyx

cdef float c_cube(float x):
    return x * x * x

my_test.pyx

from my_volume cimport c_cube

def cube(x):
    return c_cube(x)

setup.py

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

ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) + '/'

setup(
name = 'c_optimized',
ext_modules=[
    Extension('my_test',
        sources=[ROOT_DIR + 'test/my_test.pyx',
                 ROOT_DIR + 'test/my_volume.pyx',
                ],
        extra_compile_args=["-std=c++11", "-O3"],
        include_dirs=[ROOT_DIR],
        extra_link_args=["-std=c++11", "-O3"],
        language='c++'),

],
cmdclass = {'build_ext': build_ext},
script_args = ['build_ext'],
options = {'build_ext': {'inplace': True, 'force': True}},

)

test_c.py

...
print(cpp.cube(3))

Build errors:

1) multiple definition of PyInit_my_test' build/temp.linux-x86_64-3.5/home/user/projects/python/service_core/c_optimized/test/my_test.o:/home/user/projects/python/service_core/c_optimized/test/my_test.cpp:783: first defined here build/temp.linux-x86_64-3.5/home/user/projects/python/service_core/c_optimized/test/my_volume.o: In functionPyInit_my_test':

2) multiple definition of `__pyx_module_is_main_my_test' build/temp.linux-x86_64-3.5/home/user/projects/python/service_core/c_optimized/test/my_test.o:/home/user/projects/python/service_core/c_optimized/test/my_test.cpp:1280: first defined here

What's wrong with my code? I need pxd/pyx common files that I can include to other. How can I include code from one pyx file to another?

K. L.
  • 1
  • 3
  • What you are trying to do is to put multiple pyx-files into one extension (https://stackoverflow.com/q/30157363/5769463). It is not what you want/should do: Create an extension per pyx-file. – ead Mar 23 '20 at 13:36
  • What about https://cython.readthedocs.io/en/latest/src/userguide/sharing_declarations.html (Sharing C Functions)? They do what i need. – K. L. Mar 23 '20 at 14:04
  • They use `setup(ext_modules=cythonize(["landscaping.pyx", "shrubbing.pyx"]))`, which creates two different extensions - this is quite a difference to you setup.py. – ead Mar 23 '20 at 14:12
  • So, no way to build 2 pyx in one extension and use pxd as header? – K. L. Mar 23 '20 at 15:03
  • The question is not whether you could (you can, see the link above), but whether you should. I think rather not. – ead Mar 23 '20 at 15:49

1 Answers1

0

I found part solution. setup.py

from Cython.Build import cythonize
import os

ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) + '/'

test = Extension('test',    
            sources=[ROOT_DIR + 'test/my_test.pyx'],
            extra_compile_args=["-std=c++11", "-O3"],
            extra_link_args=["-std=c++11", "-O3"],
            include_dirs=[ROOT_DIR, '.'],
            language='c++')

volume = Extension('volume',
            sources=[ROOT_DIR + 'test/my_volume.pyx'],
            extra_compile_args=["-std=c++11", "-O3"],
            extra_link_args=["-std=c++11", "-O3"],
            include_dirs=[ROOT_DIR, '.'],
            language='c++')

setup(
     ext_modules=cythonize([test, volume])
)

This solution gives control over compilation:

x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/home/user/projects/python/service_core/c_optimized/ -I. -I/usr/include/python3.5m -c /home/user/projects/python/service_core/c_optimized/test/my_test.cpp -o build/temp.linux-x86_64-3.5/home/user/projects/python/service_core/c_optimized/test/my_test.o -std=c++11 -O3
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
x86_64-linux-gnu-g++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.5/home/user/projects/python/service_core/c_optimized/test/my_test.o -o /home/user/projects/python/service_core/test.cpython-35m-x86_64-linux-gnu.so -std=c++11 -O3
building 'volume' extension
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/home/user/projects/python/service_core/c_optimized/ -I. -I/usr/include/python3.5m -c /home/user/projects/python/service_core/c_optimized/test/my_volume.cpp -o build/temp.linux-x86_64-3.5/home/user/projects/python/service_core/c_optimized/test/my_volume.o -std=c++11 -O3
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
x86_64-linux-gnu-g++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.5/home/user/projects/python/service_core/c_optimized/test/my_volume.o -o /home/user/projects/python/service_core/volume.cpython-35m-x86_64-linux-gnu.so -std=c++11 -O3
K. L.
  • 1
  • 3