拡張機能をコンパイルすると、リンカー エラーが表示されます/usr/bin/ld: /usr/lib: No such file: File format not recognized
。/usr/lib
奇妙な理由で gcc コマンドにファイルとして追加されていることに気付きました。コマンドとその出力は次のとおりです。
python setup.py build
running build
running build_ext
building 'test' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes /usr/include -fPIC -I/usr/include/python2.6 -c test.c -o build/temp.linux-x86_64-2.6/test.o
gcc: /usr/include: linker input file unused because linking not done
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions /usr/lib /usr/include build/temp.linux-x86_64-2.6/test.o -o build/lib.linux-x86_64-2.6/test.so
/usr/bin/ld: /usr/lib: No such file: File format not recognized
collect2: ld returned 1 exit status
error: command 'gcc' failed with exit status 1
setup.py
私はこれを残して、基本以外のすべてからを取り除きました:
from distutils.core import setup, Extension
setup(
name = "test",
ext_modules =
[
Extension("test",
sources = [
"test.c"
]
)
]
)
そして、ここにあるtest.c
:
#include <Python.h>
static PyObject *
py_run_executable(PyObject *self, PyObject *args)
{
char *file_path = NULL;
if (!PyArg_ParseTuple(args, "s", &file_path))
return NULL;
return PyInt_FromSize_t((size_t) 1);
}
PyDoc_STRVAR(pet_cpu__doc__, "Testing module");
PyDoc_STRVAR(run_executable__doc__, "Function doc");
static PyMethodDef pet_cpu_methods[] = {
{"run_executable", py_run_executable, METH_VARARGS, run_executable__doc__},
{NULL, NULL}
};
PyMODINIT_FUNC
initpet_cpu(void)
{
Py_InitModule3("test", pet_cpu_methods, pet_cpu__doc__);
}
試したことと感想
ご覧のとおり、私は拡張機能自体の問題を排除し、最小限のものを維持しようとしました。python
またはを使用してパスを設定する方法に、何らかの構成上の問題があるようですdistutils
。失敗して削除するコマンドを実行すると/usr/lib /usr/include
:
$ gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions build/temp.linux-x86_64-2.6/test.o -o build/lib.linux-x86_64-2.6/test.so
このコマンドを実行すると、リンクが実行され、*.so
が作成されます。主な問題はdistutils
、コンパイル自体ではなく、にあるようです。
私の質問は、なぜこれら 2 つのパスが追加されたのですか?
これを実行しているコンピューターは、python 2.6.6 を使用した新しい Debian 6.0 インストールです。