Как я могу импортировать pyx / pxd в Cython, как cpp / h в c ++? - PullRequest
0 голосов
/ 23 марта 2020

У меня есть:

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))

Ошибки сборки:

1) множественное определение 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 function PyInit_my_test ':

2) множественное определение `__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: впервые определено здесь

Что не так с моим кодом? Мне нужны общие файлы pxd / pyx, которые я могу включить в другие. Как я могу включить код из одного файла pyx в другой?

1 Ответ

0 голосов
/ 24 марта 2020

Я нашел решение для части. 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])
)

Это решение дает контроль над компиляцией:

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
...