5

次のように定義された 2 つの関数を使用して、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 Extensionbuld_extfor build_ext、およびもう 1 つ修正したと思いますが、覚えていません。

numpy と openmp のものはあなたの問題とは関係がないので取り除きました。邪魔にならないようにする方が簡単でした。

これらすべてを修正して実際にセットアップを実行すると、次の問題がすぐに明らかになります。

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

test.cからビルドされたファイルでファイルを上書きするかtest.pyx、運が良ければ、生成されたファイルを無視して、あたかも の cython コンパイル出力であるかのようにtest.c既存のファイルを使用します。いずれにせよ、同じファイルを 2 回コンパイルし、結果をリンクしようとしているため、複数の定義が作成されています。test.ctest.pyx

そのファイルにデフォルト以外の名前を使用するように Cython を構成するか、より単純に、通常の命名規則に従い、最初にtest.pyxa を使用しようとする a を持たないようにすることができます。test.c

そう:


ctest.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()

setup.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 に答える