Python distutils not using the correct version of gcc

I am trying to compile a package on Mac OSX 10.6.5. The installation of the script package is based on distutils. The problem is that gcc defaults to gcc version 4.2 (I determined this by simply running gcc -version in a terminal window), but when I run "build python setup.py", I see in the output that distutils selects gcc -4.0 instead 4.2. This is a big problem because the code used requires gcc> = 4.2. I do not have administrator rights on this computer, since workaroud, I created some symbolic links that send gcc-4.0 to gcc-4.2. As a result, the code is compiled, but the generated .so files do not work (when I try to import them into python, I get errors complaining about the missing init function in the shared object).

I tried to compile this code on another mac (10.6.6), and it works like a charm: distutils selects 4.2 without being forced to do this, and I can import the generated shared object without any problems. So, what I would like to do is compile the code on my computer without doing this cheating symlink ... I just want distutils to automatically select 4.2. I tried to take the .so files that compiled correctly and transfer them to my computer, but this fails for a number of reasons (they are associated with libraries that are not present on my computer / this is another version that they installed).

Does anyone have any advice?

Thanks Josh

+6
source share
4 answers

To force distutils to use a separate compiler, you can override several variables through the environment. First find out which distutils use by default:

>>> from distutils import sysconfig >>> sysconfig.get_config_var('LDSHARED') 'gcc-4.0 -Wl,-F. -bundle -undefined dynamic_lookup' >>> sysconfig.get_config_var('CC') 'gcc-4.0' 

Then you need to override them, replacing the version of gcc that you would like to use:

 % LDSHARED="gcc-4.2 -Wl,-F. -bundle -undefined dynamic_lookup" CC=gcc-4.2 \ /usr/bin/python setup.py build_ext 

Keep in mind that the default sysconfig values ​​are extracted from the Makefile that was originally used to compile python, so using them may lead to unexpected results:

 >>> path = sysconfig.get_python_lib(plat_specific=1, standard_lib=1) >>> os.path.join(path, 'config', 'Makefile') '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/config/Makefile' 
+16
source

Have you tried setting a custom CC environment variable?

 CC=gcc-4.2 python setup.py build 
+4
source

You do not say which versions of Python you are using, but the likelihood that Python by default on the computer you are using is not supplied by Apple Python 2.6.1, most likely installed from python.org. Try running the script construct with /usr/bin/python2.6 , which should be supplied by Apple Python 2.6; which uses gcc-4.2 .

When you use Python Distutils (usually by running setup.py script or using easy_install ), Distutils tries to make sure that any C extension modules in the package you install will be compiled with the same compiler version and with compatible compilation options (CPU arch, ABI, etc.) that were used to create Python itself. Traditionally, most python.org installers for OS X provide universal Python that works with multiple versions of OS X and multiple processors. That's why they are built using gcc-4.0. If you need to use gcc-4.2 for any other library, then the safest is to use Python, which was built with gcc-4.2. The Apple Python embedded system on OS X 10.6 is built like this. In addition, for the most recent releases of Python (2.7.1 and 3.2), python.org provides a second option to install OS X for OS X 10.6, which is also built using gcc-4.2. But if you do not have administrator access to your computer, this is not an option in any case.

You can see which python is used by default:

 which python 
+3
source

You can either configure distutils.cfg (see here ), or you can pass command line arguments like -compiler = gcc42 (not sure about the latter).

+1
source

All Articles