gpt4 book ai didi

c++ - Cython:为什么是 "duplicate symbol"?

转载 作者:搜寻专家 更新时间:2023-10-31 01:32:54 24 4
gpt4 key购买 nike

我是 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?我认为 primes.pyx 被 Cython 编译为 primes.cpp 作为构建的一部分,因此存在文件名冲突。

关于c++ - Cython:为什么是 "duplicate symbol"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42260605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com