我是Cython的新手,所以如果这是一个菜鸟问题,请原谅我。我正在尝试将primes example修改为纯c++(.h / .cpp)实现,然后将其包装为.pyx文件。
primes.h:
#include <list>
std::list<int> primes2_c(int maxn);
primes.cpp :
#include "primes.h"
#include <list>
std::list<int> primes2_c(int maxn) {
int n;
std::list<int> ret;
n = 2;
while (n < maxn) {
auto it = ret.begin();
for (; it != ret.end() && n % *it != 0; it++);
if (it == ret.end()) {
ret.push_back(n);
}
n++;
}
return ret;
}
primes.pyx :
cdef extern from "primes.h":
list[int] primes2_c(int)
def primes2(kmax):
ret = primes2_c(kmax)
return ret
primes.setup :
from distutils.core import setup, Extension
from Cython.Build import cythonize
setup(
name='primes',
ext_modules=cythonize(Extension(
'primes',
['primes.pyx', 'primes.cpp'],
language='c++',
extra_compile_args=["-std=c++11"],
extra_link_args=["-std=c++11"],
))
)
然后,我运行编译并看到此错误:
[cython (master)]$ python primes.setup build_ext --inplace
running build_ext
building 'primes' extension
/usr/bin/clang -fno-strict-aliasing -Wsign-compare -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -arch i386 -arch x86_64 -g -I/Users/.../venv/cython/include -I/Library/Frameworks/Python.framework/Versions/3.5/include/python3.5m -c primes.cpp -o build/temp.macosx-10.6-intel-3.5/primes.o -std=c++11
/usr/bin/clang -fno-strict-aliasing -Wsign-compare -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -arch i386 -arch x86_64 -g -I/Users/.../venv/cython/include -I/Library/Frameworks/Python.framework/Versions/3.5/include/python3.5m -c primes.cpp -o build/temp.macosx-10.6-intel-3.5/primes.o -std=c++11
/usr/bin/clang++ -bundle -undefined dynamic_lookup -arch i386 -arch x86_64 -g build/temp.macosx-10.6-intel-3.5/primes.o build/temp.macosx-10.6-intel-3.5/primes.o -o /Users/.../cython/primes.cpython-35m-darwin.so -std=c++11
clang: warning: libstdc++ is deprecated; move to libc++ with a minimum deployment target of OS X 10.9
clang: warning: libstdc++ is deprecated; move to libc++ with a minimum deployment target of OS X 10.9
duplicate symbol __Z9primes2_ci in:
build/temp.macosx-10.6-intel-3.5/primes.o
ld: 1 duplicate symbol for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command '/usr/bin/clang++' failed with exit status 1
[cython (master)]$
我的理解是 primes.pyx 仅读取
primes2_c()
的API,但不读取实现。因此,为同一功能提供的唯一实现应该是从对 primes.setup 中的 primes.cpp 的引用。 最佳答案
您能否将primes.cpp
重命名为primesc.cpp
?我认为cyton将primes.pyx
编译为primes.cpp
作为构建的一部分,因此存在文件名冲突。
关于c++ - Cython:为什么要 “duplicate symbol”?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42260605/