[Python-Dev] Linux Python linking with G++?

"Martin v. Löwis" martin at v.loewis.de
Fri Jul 8 00:13:13 CEST 2005


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++.

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.

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.

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

Regards,
Martin


More information about the Python-Dev mailing list