问题
In an open source project1 we have Python/Cython and C/C++ modules mixed with one C++ library using the Python C API. The API changed only a few function's names from 2 to 3. Assume the library is written without those functions. Will it link to Python3 if compiled with Python2, and vice versa? Is this prevented by macros in the API headers?
Having a library binary that may link to both would spare us major packaging hassles.
回答1:
No, it wouldn't work. Don't try it.
Binary modules are not guaranteed to be binary-portable even say from 3.5 to 3.6. If you're lucky, then there is some mechanism that will prohibit you from doing this insanity. If however you manage to link the library somehow, there will be some subtle differences that will cause serious bugs, such as the layout of PyObject
changing and so forth.
The Python interface must be recompiled for the exact Python version. Source compatibility between Python 2 and 3 is a different thing and is relatively easy to achieve.
来源:https://stackoverflow.com/questions/44322187/binary-using-both-python-c-api-version-2-and-3