Python 使用distutils编译:/usr/lib作为要编译的文件添加

Python 使用distutils编译:/usr/lib作为要编译的文件添加,python,linux,gcc,distutils,Python,Linux,Gcc,Distutils,编译扩展名时,我收到一个链接器错误,提示/usr/bin/ld:/usr/lib:没有这样的文件:无法识别文件格式。我注意到由于一些奇怪的原因,/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

编译扩展名时,我收到一个链接器错误,提示
/usr/bin/ld:/usr/lib:没有这样的文件:无法识别文件格式
。我注意到由于一些奇怪的原因,
/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"
             ]
         )
         ]
)
下面是
测试.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__);
 }
运行此命令将执行链接并生成
*。因此
。似乎主要的问题在于
distutils
,而不是编译本身

我的问题是,为什么要添加这两条路径?


运行这台计算机的是一台新的Debian6.0,安装了Python2.6.6

原来问题出在我的用户的环境路径上。我试着运行与root相同的命令,其中点文件根本没有被更改,效果很好。

问题是,当
cxflags
CFLAGS
LDFLAGS
.profile
中手动设置时,它们包含在
gcc
命令中。以下是我的
.profile
中的问题行:

export CFLAGS="/usr/include"   
export CXXFLAGS="/usr/include"  
export LDFLAGS="/usr/lib"
之所以添加这些,是因为我需要设置路径来编译另一个不使用
autoconf
configure
-脚本的项目


我把这个作为答案。如果有人提出了将这些路径添加到命令的确切原因,我将接受这个答案

如果环境变量
CFLAGS
可用,则应遵守该变量


如果您查看
调整编译器/链接器标志下的内容,它清楚地被描述为环境变量的默认行为。

我遇到了同样的问题。正如@rzetterberg所描述的,这是
LDFLAGS
的问题

原来我在
~/.bashrc
中将
LDFLAGS
设置为:

导出LDFLAGS=/some/path/cuda/cuda-9.0/cuda/lib64

并应修改为:

导出LDFLAGS=-L/some/path/cuda/cuda-9.0/cuda/lib64

谢谢你,HonkyTonk。显然,我需要阅读更多关于
CLFAGS
如何被广泛使用以及环境变量的知识。@rzetterberg很高兴我能提供帮助。
export CFLAGS="/usr/include"   
export CXXFLAGS="/usr/include"  
export LDFLAGS="/usr/lib"