Warm tip: This article is reproduced from stackoverflow.com, please click
cross-compiling mariadb qt-creator raspberry-pi3

How to cross compile a RPI3 (Stretch) with mariadb10 installed for use with QTCreator?

发布于 2020-05-28 10:24:09

I am working on cross compiling a QTCreator (5.9) C++ program to be used exclusively on a RPi 3 running Stretch and a mariadb10. I'm having a problem getting the MYSQL to be available in the compiled. Host machine is running Ubuntu 18.

I've run the following lines to create the cross compile library. All seems fine except that MYSQL is not available:

cd ~/raspi3;
git clone https://github.com/raspberrypi/tools;
git clone git://code.qt.io/qt/qtbase.git -b 5.9;
cp ~/Desktop/sysroot-relativelinks.py ~/raspi3; #Copy previously downloaded file. No longer available as of 2/5/2019 at recommended https://raw.githubusercontent.com/riscv/riscv-poky/priv-1.10/scripts/sysroot-relativelinks.py
chmod +x sysroot-relativelinks.py;
mkdir sysroot/usr sysroot/opt;
rsync -avz pi@$ip:/lib sysroot;
rsync -avz pi@$ip:/usr/include sysroot/usr;
rsync -avz pi@$ip:/usr/lib sysroot/usr;
rsync -avz pi@$ip:/opt/vc sysroot/opt;
python ./sysroot-relativelinks.py sysroot;
cd qtbase;
./configure -release -opengl es2 -device linux-rasp-pi3-g++ -I /usr/arm-linux-gnueabihf -device-option CROSS_COMPILE=~/raspi3/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf- -sql-mysql MYSQL_INCDIR=~/raspi3/sysroot/usr/include/mysql MYSQL_LIBDIR=~/raspi3/sysroot/usr/lib/arm-linux-gnueabihf MYSQL_LIBS="-lmariadbclient" -sysroot ~/raspi3/sysroot -opensource -confirm-license -make libs -prefix /usr/local/qt5pi -extprefix ~/raspi3/qt5pi -hostprefix ~/raspi3/qt5 -v -nomake examples -nomake tests -nomake tools -no-use-gold-linker -recheck-all;

The above ./configure runs and produces the following report (excerpt):

Qt Sql:
  DB2 (IBM) .............................. no
  InterBase .............................. no
  MySql .................................. no
  OCI (Oracle) ........................... no
  ODBC ................................... yes
  PostgreSQL ............................. yes
  SQLite2 ................................ no
  SQLite ................................. yes
    Using system provided SQLite ......... no
  TDS (Sybase) ........................... yes

Note: Also available for Linux: linux-clang linux-icc

Note: PKG_CONFIG_LIBDIR automatically set to /home/max/raspi3/sysroot/usr/lib/pkgconfig:/home/max/raspi3/sysroot/usr/share/pkgconfig:/home/max/raspi3/sysroot/usr/lib/arm-linux-gnueabihf/pkgconfig

Note: PKG_CONFIG_SYSROOT_DIR automatically set to /home/max/raspi3/sysroot

Note: Dropped compiler flags '-pthread' when detecting library 'glib'.

ERROR: Feature 'sql-mysql' was enabled, but the pre-condition 'libs.mysql' failed.

Note: I've also tried removing the MYSQL_INCDIR, MYSQL_LIBDIR and MYSQL_LIBS with no change in results

When I compile and run the program created in QTCreator, I can see there is no available QMYSQL driver and no database connection:

QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QODBC QODBC3 QPSQL QPSQL7 QTDS QTDS7
FAILED TO CONNECT TO DB

I've hunted around ALOT and tried just about everything sensible that I've come across. Can anyone tell me what might be wrong in the ./compile that might be causing the issue?

Questioner
Max
Viewed
118
Banshee10000 2020-03-12 19:45

Okay so I have succeeded and have a working solution. I followed this tutorial to setup a cross compiling environment to the letter https://mechatronicsblog.com/cross-compile-and-deploy-qt-5-12-for-raspberry-pi/

However, Ive made some changes along the way that was excluded with MySQL in mind. Firstly on the Raspberry Pi side I installed additional packages for MariaDB as it replace MySQL in Debian Buster. I also used the Respbian lite version (Buster) on my Raspberry Pi 3 B+.

My Linux Host is: Linux Mint 19.3 in a VM. Because Cinnamon is awesome (^^,)

On the Raspberry Pi SSH into it and install.

sudo apt install mariadb-client mariadb-common mysql-common libmariadb-dev-compat libmariadbclient-dev libqt5sql5-mysql libqt5sql5 libqt5sql5-sqlite

As a side note. Not sure if its needed but on the Raspberry Pi I have also installed libqt5core and a bunch of other Qt related libs. Networking, widgets, core and so on.

The libmariadb-dev-compat package will create all the links necessary for compatibility between mariadb and mysql. Then, when you call to qt configure script you must indicate the path of the mysql (mariadb) libraries:

Exit the Raspberry Pi SSH session. On the Host PC.
Make sure you run rsync AFTER installing all the MariaDB libs etc. As per the tutorial.

rsync -avz pi@raspberrypi_ip:/lib sysroot
rsync -avz pi@raspberrypi_ip:/usr/include sysroot/usr
rsync -avz pi@raspberrypi_ip:/usr/lib sysroot/usr
rsync -avz pi@raspberrypi_ip:/opt/vc sysroot/opt

Now when it comes to configure Qt-Everywhere (I used Qt 5.12.5) we need to add MySQL to the make configuration

Here is my exact configuration script for Qt 5.12.5 to compile it from source with MySQL(MariaDB) Driver.

./configure -release -opengl es2 -device linux-rasp-pi-g++ -device-option CROSS_COMPILE=~/raspi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf- -sysroot ~/raspi/sysroot -opensource -confirm-license -skip qtwayland -skip qtlocation -skip qtscript -make libs -prefix /usr/local/qt5pi -extprefix ~/raspi/qt5pi -hostprefix ~/raspi/qt5 -no-use-gold-linker -v -no-gbm -sql-mysql MYSQL_INCDIR=~/raspi/sysroot/usr/include/mysql MYSQL_LIBDIR=~/raspi/sysroot/usr/lib/arm-linux-gnueabihf

after the configuration step, check to verify that MySQL is set to yes. Something like:

 Qt Sql Drivers:
 DB2 (IBM) .............................. no
 InterBase .............................. no
 MySql .................................. yes
 OCI (Oracle) ........................... no
 ODBC ................................... no
 PostgreSQL ............................. yes
 SQLite2 ................................ no
 SQLite ................................. yes

Continue on with the Tutorial listed until you get to adding the raspberry as a device in Qt Creator....

Another thing that cough me late last night after compiling for ~24 hours

In Qt 5.14.1 when setting up devices (Raspberry Pi) in Qt creator you have to use a SSH key now. The password option is not there anymore. Have Qt generate the SSH key and copy that to your raspberry pi via SCP or what ever. The "qtc_id.pub" file goes into /home/pi/.ssh then create a file called "authorized_keys" in the .ssh folder.

touch ~/.ssh/authorized_keys

next

cat "qtc_id.pub" >> authorized_keys

This is just to copy the name qtc_id.pub into that file as plain text. save the file and exit. Then

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

You should now be able to connect to the Raspberry Pi when setting it up in Qt creator as described in the tutorial above.

ALSO: I tried to build this with the latest Qt 5.14.1 source. Its failed with multiple issues that I just don't have the time or energy to try and mess with. I just wanna get to programming the project.

I hope this helps you @Max and others. For me I was successful. This has been a struggle and a half. Building Qt from source and setting up a raspberry Pi cross compile environment has been the most frustrating thing Ive experienced in a very long time. The documentation is outdated and or very vague most of the time. Way to many things that can go wrong but for now this is the only way I could get it all to work. I still don't know how to compile MySQL after one has compiled Qt already. As in just compile the MySQL plugin drive. No Idea.

Greets form South Africa