Building Statically Linked Qt Programs f ...

Building Statically Linked Qt Programs for GNU/Linux with chroot and debootstrap

Feb 21, 2022

Building binaries for Linux distributions is difficult, mainly due to a version requirement for glibc. When I tried to statically link a program with Qt's libraries on Ubuntu 21.04 and run it on Ubuntu 20.04, it failed because it required a newer version of glibc. I recommend using Linux ldd to print shared object dependencies: it will tell you if it dependent libraries are missing or the ones it can find are the wrong version.

Fortunately, there is solution, and that is to use chroot and debootstrap to initialize a root filesystem with an older version of Ubuntu, in my case, Bionic or 18.04. I've tried to describe the steps I took to build a statically linked program for Bionic, that would run with older Linux distributions.

My reason for choosing Ubuntu 18.04, and not an older older version to widen compatibility, is because a well known multimedia software developer, Rui Nano Capela (rncbc) provided pre-built packages containing static libraries and headers for Qt 6.2.3: https://launchpad.net/~rncbc/+archive/ubuntu/qt6.2-static-bionic.

First, I created the root directory in /var/chroot/bionic, and I then ran:

sudo debootstrap bionic /var/chroot/bionic

So, after initializing and installing Bionic to my chosen root directory, I installed software-properties-common to be able to add user repositories, and then added rncbc's repository:

sudo add-apt-repository ppa:rncbc/qt6.2-static-bionic
sudo apt-get update

Installing the essential tools was next, and I did so by installing the build-essentials package.

I then installed the latest version of cmake, by downloading the latest source archive, running ./configure and then make.

Now, I needed to expose my project directory by first creating the directory in my jail:

mkdir /projects

And then executing this on my host machine:

sudo mount --bind /home/ben/Documents/projects /var/chroot/projects

After that, I could now progress to building my program in my jail:

cmake -B build-bionic-sysroot -DCMAKE_PREFIX_PATH=/opt/qt6.2-static/
cmake --build build-bionic-sysroot

And now, after building completed, it will I can run the binary on Ubuntu 20.04.

I hope this was useful, I look forward to your questions and comments.

Enjoy this post?

Buy Ben Cottrell a coffee

More from Ben Cottrell