在编译我的扩展时,我收到一个链接器错误,它说/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
从所有的东西中剥离出来,只剩下一些基本的东西:
这是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
,而不是编译本身。在
我的问题是,为什么要添加这两条路径?
运行此程序的计算机是一个新的Debian6.0安装程序和Python2.6.6。在
如果环境变量
CFLAGS
可用,则会考虑该变量。在如果您在
Tweaking compiler/linker flags
@http://docs.python.org/install/下查看,它被清楚地描述为环境变量的默认行为。在结果发现问题出在我的用户的环境路径上。我试着运行与root相同的命令,这些命令的点文件根本没有改变,而且运行良好。
问题是当
CXXFLAGS
,CFLAGS
,LDFLAGS
被手动设置到.profile
中时,它们被包含在gcc
命令中。以下是我的.profile
中的问题行:之所以添加这些是因为我需要路径集来编译另一个不使用
autoconf
或configure
-脚本的项目。在我把这个作为答案。如果有人提出了
distutils
将这些路径添加到命令的原因,我会接受这个答案。在我遇到了同样的问题。正如@rzetterberg所描述的,这是
LDFLAGS
的问题。在结果我将
~/.bashrc
中的LDFLAGS
设置为:export LDFLAGS=/some/path/cuda/cuda-9.0/cuda/lib64
修改为:
export LDFLAGS=-L/some/path/cuda/cuda-9.0/cuda/lib64
相关问题 更多 >
编程相关推荐