Build and install MySQL driver/connector on cross-compiled system
-
I've established a cross-compilation workflow, whereby I write, build and deploy my Qt application on a Linux desktop to an embedded Linux device.
I'm in the process of setting up database connections, and have SQLite database functionality working nicely. I'm now looking to build a MySQL function, which will send data from my embedded device to MySQL tables on my website. I don't currently have MySQL drivers setup/installed, as evidenced by the following error when trying to add a MySQL database connection (QSqlDatabase::addDatabase("QMYSQL")):
QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: available drivers: QSQLITE QSQLITE2 QODBC QODBC3 QPSQL QPSQL7 QTDS QTDS7
I've read a number of forum posts, articles, etc. on building the necessary drivers for a MySQL connection, but am confused on how this works on a cross-compiled platform. Do I build the necessary drivers on my workstation, and then also build (natively) on the embedded device, or do I build once on the workstation and copy files across, etc.? My understanding is I have to rebuild the mysql.pro file with additional INCLUDE statements for the c++ connectors I download from the MySQL website.
Any pointers or guidance on this would be greatly appreciated as always.
-
I've downloaded and extracted the c connector .tar.gz from the MySQL website, so now have the following directory available on the embedded device:
~/mysql-connector-c-6.1.11-linux-glibc2.12-i686
The contents of this directory are as follows:
bin COPYING docs include lib README
Inside the lib directory is the following files, which I believe are the important ones:
libmysqlclient.a
libmysqlclient.so.18
libmysqlclient.so
libmysqlclient.so.18.4.From what I've read, the libmysqlclient.so file should be in the sqldrivers directory on the embedded device. If I navigate to /usr/local/qtdir/plugins/sqldrivers on the embedded device, I have the following files:
libqsqlite2.so
libqsqlite.so
libqsqlodbc.so
libqsqlpsql.so
libqsqltds.soWhat I don't understand, is how I 'build' mysql on the embedded device. On a similar note, how would I 'build' mysql on my workstation so I can also access the MySQL connection for testing, etc.
-
Conversely, how can I modify my initial cross-compile build process to ensure MySQL drivers and required headers, shared libraries, etc. are deployed to the embedded device? I think I'd rather start from scratch and rebuild both my workstation OS and the embedded device OS with the correct build flags, dependencies etc.
-
I've not made any progress on this issue unfortunately. I've come to the conclusion that I should figure out the process for including the MySQL driver in the cross-compile ./configure, so it becomes part of my normal workflow, rather than trying to build and deploy the ARM MySQL files once I've built Qt.
With that said, this SO post seems to have addressed this issue. The solution (it would appear), was to use the following ./configure command:
./configure -prefix [...] -xplatform win32-g++ -device-option CROSS_COMPILE=/usr/bin/i686-w64-mingw32- -opengl desktop -no-sql-odbc -sql-sqlite -plugin-sql-mysql MYSQL_PREFIX=[...] -enable-shared -nomake examples -nomake tests -release -recheck-all
This particular case was for cross-compiling a Windows target on Linux, hence the win32-g++ device and use of minw32. The ./configure command I use to cross-compile for my target device (RPi2 in this case) is as follows:
configure -opengl es2 -device linux-rasp-pi2-g++ -device-option CROSS_COMPILE=/opt/qt5pi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf- -sysroot /opt/qt5pi/sysroot -prefix /usr/local/qt5pi -opensource -confirm-license -optimized-qmake -reduce-exports -release -make libs -nomake examples -no-use-gold-linker -eglfs
So, am I correct in my interpretation that my modified ./configure command should be this:
configure -opengl es2 -device linux-rasp-pi2-g++ -device-option CROSS_COMPILE=/opt/qt5pi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf- -sysroot /opt/qt5pi/sysroot -prefix /usr/local/qt5pi -opensource -confirm-license -optimized-qmake -reduce-exports -release -make libs -nomake examples -no-use-gold-linker -eglfs -plugin-sql-mysql MYSQL_PREFIX=/opt/qt5pi/mysql -recheck-all
As for MYSQL_PREFIX section; I simply download the MySQL developer files and put them in a directory (I've used /opt/qt5pi/mysql above as an example), then point the ./configure command to the files? Do I also need to download files/do anything on the RPi directly, or is including the files on the Linux workstation during the configure and build process sufficient?
I've based this understanding on the Qt documents, namely this section:
The configure script cannot detect the necessary libraries and include files if they are not in the standard paths, so it may be necessary to specify these paths using the *_INCDIR=, *_LIBDIR=, or *_PREFIX= command-line options. For example, if your MySQL files are installed in /usr/local/mysql (or in C:\mysql on Windows), then pass the following parameter to configure: MYSQL_PREFIX=/usr/local/mysql (or MYSQL_PREFIX=C:\mysql for Windows). The particulars for each driver are explained below. Due to the practicalities of dealing with external dependencies, only the SQLite3 plugin is shipped with binary builds of Qt. To be able to add additional drivers to the Qt installation without re-building all of Qt, it is possible to configure and build the qtbase/src/plugins/sqldrivers directory outside of a full Qt build directory. Note that it is not possible to configure each driver separately, only all of them at once. Drivers can be built separately, though. If the Qt build is configured with -prefix, it is necessary to install the plugins after building them, too. For example: cd $QTDIR/qtbase/src/plugins/sqldrivers/mysql make install
Given the above, and the fact that my ./configure command uses the -prefix flag, do I then need to make install the mysql files directly (as per the last two lines of the quote above)? If so, is that on the Linux workstation or the target Linux device (RPi)?
Once that's said and done, and assuming the cross-compilation has been successful (i.e. can run application remotely on RPi from QtCreator on Linux workstation), how do I then run the application on the Linux workstation? I haven't specified the MySQL driver files for the Linux workstation?
Any guidance would be greatly appreciated. I'm going to wipe my Linux workstation and RPi to start fresh, as I have no idea what libraries, packages, files, etc. I've polluted both systems with at this point.
-
Is anyone able to provide some input on this issue? I've spent a number of hours researching, and have tried a number of steps, but I'm still unable to get this working.
I downloaded the MySQL c connector source .tar.gz from the MySQL website (generic Linux, architecture independent) and built and installed the package on both the target device (RPi) and host machine (Ubuntu). Both installed packages are in /usr/local/mysql. I have no idea whether I need to build and install on both the target and host machines, but at this point I'm just trying to cover all bases :D
With the packages seemingly installed at /usr/local/mysql, I believe I then need to update the /plugins/sqldrivers/mysql .pro file with qmake, so I do the following:
/opt/qt5pi/sysroot/usr/local/qt5pi/bin/qmake "INCLUDEPATH+=/usr/local/mysql/include" "LIBS+=/usr/local/mysql/lib" -o Makefile mysql.pro
I receive the error about qtsqldrivers-config.pri not being found. This is the part I'm not sure about. Which 'mysql.pro' file am I updating? As this is a cross-compiled environment, I have Qt source folders in a couple of locations; should this qmake command be editing to the Qt source folder which was used to ./configure Qt previously?
I read elsewhere that I need to rebuild qmake before attempting the above line. I tried that as well, but MySQL still comes up as .....no in the output of the ./configure command.
I'm not sure what else to try at this point, any guidance would be greatly appreciated. As mentioned in my previous post, I'm going to wipe both the host and target operating systems, so if there's a concise, proven method for including the MySQL drivers in the initial cross-compilation process I'm happy to investigate that rather than trying to either cross-compile and update the driver individually afterwards, or building and installing natively on the target device.
-
I've run the ./configure command again, this time with what seems to be a better set of flags. My configure.summary now shows MySQL as being available (.....yes) which is a step in the right direction. I'm still confused between the different directories however, as when I try an update the mysql.pro file with qmake (with the INCLUDEPATH and LIBS commands) it's looking in another directly which doesn't have the required mysql libraries/headers.
I'm going to wipe everything and try again from scratch, that way I'm hopefully less likely to point to the wrong directories.
-
For anyone playing at home, I finally got this working with a clean install and cross-compile build :D
I downloaded the mysql c connector source (generic Linux, architecture independent) and built/installed it on my target device (clean install of Linux, prior to any cross-compile setup). The resultant files are in /usr/local/mysql (as well as in some other /usr directories).
As part of my cross-compile process, I rsync a number of directories from the target device to the host, which included a number of the created mysql files. I also copied the entire /usr/local/mysql directory from my target device to the host computer's /usr/local/mysql directory, which is what I pointed to in the ./configure flag: MYSQL_PREFIX=/usr/local/mysql.
It was quite a process to get that all working, but now that I've done it, I have a much better understanding as to how to incorporate device-specific packages into a cross-compile workflow.
-
Wow , thanks for the detailed input on the process of building mysql for cross compiling. Was spending the last 2 days researching on how to cross compile, my process was quite similar to yours. I would like to clear my understanding towards the solution. Basically ,1.extract the mysql connector c tar file on the target machine . 2. Copy the libs after extraction from "target" to a custom directory on "host". 3 set the cross compiler on the "host" to link to the custom folder with the extracted mysql libs . Is that correct? I'm gonna test it out.
-
While build qt for target specifiy below things
-sql-mysql MYSQL_INCDIR={TARGET_rootfs}/usr/include/mariadb MYSQL_LIBDIR={TARGET_rootfs}/Qt5.15.3/Qt_rpi/rootfs/usr/lib/arm-linux-gnueabihf