About the --enable-shared option when building Python on Linux

When building Python on Linux, there is a configure option called --enable-shared, so I will explain it and give you some tips. I've given Python as a concrete example, but I think most of the content is common to other languages and libraries.

What is --enable-shared?

If you do not specify this option, you will have an executable file called python and a library file called libpython2.7.a (for Python 2.7) as the main binary files. If you add --enable-shared, a shared object for dynamic link called libpython2.7.so is also generated, and python will dynamically link this shared object.

Linux distributions such as Debian usually build Python with this option. This is because many programs are built to use Python, and if you link libpython2.7.a directly, you will have to update all of them when you upgrade Python.

I don't recommend --enable-shared because if you're building Python yourself, this advantage doesn't really come into play, but rather can get in the way of people who want to use multiple versions in parallel. Unless you want to use shared objects for some reason, you don't have to read the rest.

Problems when using --enable-shared

When a python executable file or a program that links Python as a library, such as mod_wsgi, becomes dependent on libpython2.7.so, the program you are running will depend on libpython2.7.so. There is a possibility that you will not use.

For example, if you build and install with --prefix = / usr / local / Python-2.7.3, you can run /usr/local/Python-2.7.3/bin/python to set and environment variables. Depending on the situation, /usr/lib/libpython2.7.so or /usr/local/Python-2.7.2/lib/libpython2.7.so may be used.

You can use the environment variable LD_LIBRARY_PATH to specify the location of the library to be loaded preferentially, but if you are using multiple versions of Python in parallel, it is troublesome to switch the environment variable yourself. If someone else is using the same server, that person can be confused.

Countermeasure 1 Set rpath

Maybe this is a straightforward approach. The dynamic linking side, such as the python executable file, will record the location of the linking libpython2.7.so with the full path.

To set the rpath, it's easy to specify LDFLAGS when configuring.

$ ./configure --prefix=$HOME/python27 --enable-shared \
    LDFLAGS=-Wl,-rpath,$HOME/python27/lib

The only downside to using rpath is the portability of copying the entire Python to another directory with cp -r etc. and rewriting the shebang in the script in bin /. In virtualenv, if libpython2.7.so is in the original location, it will continue to work, so there is no problem, but if you use virtualenv, you do not know how long the old version of Python is used and you do not want to erase it, so copy the whole thing It is not recommended for people who want to use it.

Countermeasure 2 Statically link libpython

If you need a shared object for some purpose, but the python executable doesn't need to dynamically link it, there is a way to link libpython2.7.a when building python. .. To do this, open the Makefile after configure and

BLDLIBRARY=     -L. -lpython$(VERSION)

Where it is

BLDLIBRARY=     libpython$(VERSION).a

I will rewrite it to.

The Debian package python seems to be built in a similar way, with libpython2.7.so statically linked to libpython. It seems that the shared object is PIC and the performance was poor. (This is also the reason why you can't use Python 2 and 3 in vim at the same time)

Recommended Posts

About the --enable-shared option when building Python on Linux
Building multiple Python environments on the same system
Think about how to program Python on the iPad
The world changed when I opened a big Python project (Django) on Sourcetrail (Linux)
About the Python module venv
About the ease of Python
Install the JDK on Linux
About the enumerate function (python)
Paste the link on linux
About the features of Python
Notes on building TinyEMU and booting the Linux kernel on Emscripten
Behavior when Linux less ends depending on the connection source
Building a Python environment on Mac
Install Python Pillow on Amazon Linux
Check the behavior when assigning Python
Error when building mac python environment
Building a Python environment on Ubuntu
Support when installing pillow on python3.9
[Python] What is @? (About the decorator)
Memorize the Python commentary on YouTube.
Build Python environment on Ubuntu (when pip is not the default)
Sakura Use Python on the Internet
Introduce Python 3.5.2 environment on Amazon Linux
python> os.path.join ('data','checkpoint')>'data / checkpoint' on linux
About the basics list of Python basics
About the error I encountered when trying to use Adafruit_DHT from Python on a Raspberry Pi
The --security option cannot be used with the dnf command on CentOS Linux
Building a Python3 environment with Amazon Linux2
[Python Kivy] About changing the design theme
Create ScriptableObject in Python when building ADX2
Find files like find on linux in Python
About the virtual environment of python version 3.7
[C] [python] Read with AquesTalk on Linux
Notes on building Python and pyenv on Mac
Notes on using OpenCL on Linux on the RX6800
Download files on the web with Python
Notify Slack when the linux command finishes
[Python] A progress bar on the terminal
Install Python 3.8, Pip 3.8 on EC2 (Amazon Linux 2)
Compiling the Linux kernel (Linux 5.x on Ubuntu 20.04)
How to correctly upgrade the software when building Linux (CentOS) with Vagrant ~ Using the example of upgrading from Python 2.7 to Python 3.6 ~
I stumbled on the character code when converting CSV to JSON in Python
The day when the flask server running on linux on AWS was semi-persistent (running in the background)
About Linux
About the handling of ZIP files including Japanese files when upgrading from Python2 to Python3
About Linux
About Linux
Correspondence memo when the direction key cannot be used on the python command line
About Linux
Use python on Raspberry Pi 3 and turn on the LED when it gets dark!
About Linux ①
I want to be notified when the command operation is completed on linux!
A Java programmer studied Python. (About the decorator)
[Linux] When "| (pipe)" cannot be hit on CentOS7
[2020July] Check the UDID of the iPad on Linux
About the difference between "==" and "is" in python
Note on encoding when LANG = C in Python
A note about the python version of python virtualenv
Try CIing the pushed python code on GitHub.
Introduction to Python with Atom (on the way)
A memorandum about the Python tesseract wrapper library