Cython尝试编译两次,但失败
我有一个 setup.py
文件,该文件与此处显示的文件非常相似: https://stackoverflow.com/a/49866324/4080129 。看起来像这样:
I have a setup.py
file that is very similar to the one shown here: https://stackoverflow.com/a/49866324/4080129. It looks like this:
from distutils.core import setup, Extension
from Cython.Build import cythonize
import numpy
sources = ["hs/subfolder/detect.pyx",
"hs/subfolder/Donline.cpp",
"hs/subfolder/Handler.cpp",
"hs/subfolder/Process.cpp",
"hs/subfolder/Filter.cpp",
"hs/subfolder/Localize.cpp"]
exts = [Extension(name='hs.detect',
sources=sources,
extra_compile_args=['-std=c++11', '-O3'],
include_dirs=[numpy.get_include()])]
setup(
ext_modules=cythonize(exts),
include_dirs=[numpy.get_include()]
)
有一个包含纯Python的程序包,以及一个包含Cython文件的子模块。 setup.py
在父文件夹中,而不在Cython文件夹中:
There's a package with some pure-Python, and a submodule that contains Cython files. The setup.py
is in the parent folder, not in the Cython one:
setup.py
hs/
some_python.py
subfolder/
detect.pyx
Donline.cpp
...etc
现在,setup.py正确编译了所有文件 module / submodule / file1.cpp
等,并将版本保存到 build / temp.linux-x86_64-3.6 / module / submodule / file1.o
。
但是,此后,它尝试编译名为 file1.cpp
的文件,该文件不存在(正确的文件是 module / submodule / file1.cpp
,并且已经被编译)。
Now, setup.py correctly compiles all the files module/submodule/file1.cpp
etc. and saves the build to build/temp.linux-x86_64-3.6/module/submodule/file1.o
.
However, just after that, it tries to compile a file called file1.cpp
, which doesn't exist (the correct one is module/submodule/file1.cpp
, and has already been compiled).
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -Ihs/subfolder -I/[...]/python3.6/site-packages/numpy/core/include -I/[...]/python3.6/site-packages/numpy/core/include -I/[...]/include -I/disk/scratch/miniconda/envs/my_default/include/python3.6m -c Donline.cpp -o build/temp.linux-x86_64-3.6/Donline.o -std=c++11 -O3
gcc: error: Donline.cpp: No such file or directory
gcc: fatal error: no input files
compilation terminated.
error: command 'gcc' failed with exit status 4
我很困惑,这完全阻止了我的代码编译...
I'm very confused, this completely prevents my code from compiling...
原来是 .pyx
文件包含一行
# distutils: sources = Donline.cpp Handler.cpp Process.cpp Filter.cpp Localize.cpp
告诉distutils要编译什么。我不知道它,因为它看起来很像注释行,所以我没有意识到它在那里。
which tells distutils what to compile. I wasn't aware of it, and since it looks an awful lot like a commented-out line, I didn't realise it was there.
Cython尝试编译除了 setup.py
文件中包含的内容外,也这些内容,即,两个来源列表都不覆盖另一个。显然,尽管这些源在pyx文件中的子文件夹中列出,但仍应位于相对于 setup.py
文件所在的文件的路径中,
Cython tries to compile also these, other than the ones contained in the setup.py
file, i.e. neither of the two sources list overrides the other. Apparently, these sources, despite being listed in the pyx file, which is in a subfolder, are expected to be in paths relative to the file where the setup.py
file is, or perhaps relative to the folder I'm calling python from.
无论如何,删除该行可以解决问题。
Anyway, removing the line solved the issue.