Search code examples
pythonlinuxgccdistutils

Compiling with distutils: /usr/lib is added as a file to compile


When compiling my extension I get a linker error saying /usr/bin/ld: /usr/lib: No such file: File format not recognized. I noticed /usr/lib is added as a file to the gcc command for some bizarre reason. Here are the commands and their output:

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

I stripped the setup.py from everything but the basics leaving me with this:

from distutils.core import setup, Extension

setup(
     name = "test", 
     ext_modules  =  
         [
         Extension("test",
             sources = [
             "test.c"
             ]
         )
         ]
)

And here is the 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__);
 }

What I have tried and my thoughts

As you can see I have tried to eliminate problems with the extension itself and just keep the bare minimum. It seems there some kind of configuration fault in how paths are setup with python or distutils. If I run the command which fails and remove /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

Running this command the linking is performed and the *.so is produced. It seems the main problem is with distutils and not the compilation itself.

My question is, why are these two paths added?

The computer running this is a new Debian 6.0 install with python 2.6.6.


Solution

  • The environment variable CFLAGS is respected if available.

    If you look under Tweaking compiler/linker flags @ http://docs.python.org/install/ it is clearly describes as the default behaviour in regards to environment variables.