5

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

#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 有多个定义。谁能告诉我这个问题?

4

1 回答 1

8

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

但是,如果我解决了所有明显的问题,一切都会奏效。所以,让我们来看看所有这些:

setup.py缺少顶部的导入,因此它会NameError立即失败。

接下来,有多个拼写错误——<code>Extenson for Extension, buld_extfor build_ext,我想还有一个我修正了但不记得了。

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

当您修复所有这些并实际运行设置时,下一个问题立即变得显而易见:

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

您要么用test.c从. test.pyx_ 无论哪种方式,您都在编译同一个文件两次并尝试将结果链接在一起,因此您有多个定义。test.ctest.ctest.pyx

您可以将 Cython 配置为对该文件使用非默认名称,或者更简单地说,遵循通常的命名约定,并且一开始就没有test.pyx尝试使用 atest.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
>>>

多田。

于 2013-10-08T23:33:21.957 回答