gpt4 book ai didi

python - cython编译错误: multiple definition of functions

转载 作者:太空狗 更新时间:2023-10-30 02:20:58 24 4
gpt4 key购买 nike

我创建了一个名为 test.c 的 c 文件,其中包含两个定义如下的函数:

#include<stdio.h>
void hello_1(void){
printf("hello 1\n");
}
void hello_2(void){
printf("hello 2\n");
}

之后,我按如下方式创建 test.pyx:

import cython
cdef extern void hello_1()

安装文件如下:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(cmdclass={'buld_ext':build_ext},
ext_modules=[Extension("test",["test.pyx", "test.c"],
include_dirs=[np.get_include()],
extra_compile_args=['-g', '-fopenmp'],
extra_link_args=['-g', '-fopenmp', '-pthread'])
])

当我运行安装文件时,它总是报告 hello_1 和 hello_2 有多个定义。谁能告诉我问题所在?

最佳答案

您发布的文件有很多问题,我不知道是哪一个导致了您的真实代码中的问题——特别是因为您向我们展示的代码不会,也不可能生成那些错误。

但如果我解决了所有明显的问题,一切都会正常进行。那么,让我们浏览一下所有这些:

你的 setup.py缺少顶部的导入,因此它将失败并显示 NameError立即。

接下来,有多个错别字——Extenson对于 Extension , buld_ext对于 build_ext ,我想还有一个我已修复但不记得了。

我去掉了 numpy 和 openmp 的东西,因为它与你的问题无关,而且更容易把它移开。

当您解决所有这些问题并实际运行安装程序时,下一个问题立即变得显而易见:

$ python setup.py build_ext -i
running build_ext
cythoning test.pyx to test.c

你要么覆盖你的 test.c文件包含从 test.pyx 构建的文件,或者,如果幸运的话,忽略生成的 test.c文件并使用现有的 test.c就好像它是 test.pyx 的 cython 编译输出一样.无论哪种方式,您都在编译同一个文件两次并尝试将结果链接在一起,因此您有多个定义。

您可以将 Cython 配置为对该文件使用非默认名称,或者更简单地说,遵循通常的命名约定并且没有 test.pyx试图使用 test.c首先。

所以:


ctest.c:

#include <stdio.h>
void hello_1(void){
printf("hello 1\n");
}
void hello_2(void){
printf("hello 2\n");
}

测试.pyx:

import cython
cdef extern void hello_1()

设置.py:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(cmdclass={'build_ext':build_ext},
ext_modules=[Extension("test",["test.pyx", "ctest.c"],
extra_compile_args=['-g'],
extra_link_args=['-g', '-pthread'])
])

并运行它:

$ python setup.py build_ext -i
running build_ext
cythoning test.pyx to test.c
# ...
clang: warning: argument unused during compilation: '-pthread'
$ python
>>> import test
>>>

多田。

关于python - cython编译错误: multiple definition of functions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19260253/

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