[Python-Dev] Linux Python linking with G++? (original) (raw)

David Abrahams dave at boost-consulting.com
Fri Jul 8 02:27:25 CEST 2005


"Martin v. Löwis" <martin at v.loewis.de> writes:

David Abrahams wrote:

Apparently Python on some linux distros is being linked by g++ rather than gcc, resulting in the C++ runtime library being linked into Python; this has bad consequences for compatibility between C++ extension modules and Pythons that have been built with different versions of GCC. Is this behavior intentional? It's as Skip says. According to the C++ standard, a "C++ program" is one where all translation units are written in C++. While most platforms offer interoperability between C and C++ in the sense that C libraries can be linked into C++ programs, interoperability in the other direction is not always supported, i.e. C programs may not be able to use C++ libraries. This is the specific case you are talking about: Python is written in C, yet the extension might be written in C++.

The C++ standard doesn't cover interoperability with 'C', or dynamic linking (we on the C++ committee are working to fix that, but for now that is the case) and it doesn't cover dynamic loading without linking, which is what happens when Python loads an extension written in C++. You can't appeal to the standard for the rules about what needs to be done. All this stuff is entirely implementation-dependent.

Now, on some of these implementations, things can be fixed by writing main() as a C++ translation unit, and compiling it with the C++ compiler. Then, Python itself is a C library to this C++ program, and the extension modules are C++ libraries. Then everything works fine.

C++ extension modules work fine even when main() is a 'C' program on Linux/GCC. The only reason that main would have to be a C++ program on this platform and compiler is if there were C++ translation units linked into it (not loaded as in with dlopen). Since whoever writes the Makefile for Python also controls what's being linked into it, there's no reason to speculate and make Python a C++ program based on what might be linked in.

To support this, main() must be a C++ program. Python compiles main using the C++ compiler if configure thinks this is necessary.

Doing so is necessary for example on systems using the G++ collect2 mechanism for static initializers: compiling main with g++ leads _to a call to main(), which is then created by collect2.

Extension modules that get loaded with dlopen don't get their static initializers run by __main() anyway.

configure thinks that using CXX for linking is necessary if compiling a program using CXX and linking it using CC fails.

That might be the right thing to do for some programs, but AFAICT that's the wrong logic to use for Python.

-- Dave Abrahams Boost Consulting www.boost-consulting.com



More information about the Python-Dev mailing list