CentOS7.6 configuration Qt5.13+MySQL5.7 development environment (including packaging method)

1 Virtual machine installation

In the VMware virtual machine installation of CentOS7.6x64, in the software selection->basic environment, select "GNOME desktop" and do not check any other additional components, which can largely prevent the GCC version and Qt version from being too old and difficult to uninstall and replace trouble (full GCC and Qt versions do not exist in the base environment of the GNOME desktop type).

2 GCC upgrade (gcc4.8.5->gcc 9.3)

In Qt's official platform support, Qt5.13 Red Hat Enterprise Linux 7.x requires GCC 5.3.1 (via devtoolset-4). However, the current devtoolset-4 source is gone, and it starts directly with 7.

The latest version of gcc from the official source of CentOS 7 is 4.8.5, released in 2015, which is old and does not support c++14. To compile c++14 and above projects, you must upgrade the existing version or install a higher version of gcc.

There are two solutions: manual compilation (you can also copy from other machines or download from the Internet), or install from source.

Compiling from source code is not recommended in most cases, not only because various dependency problems often occur during the compilation process and need to be solved manually, but also because the software upgrade needs to be done again, which is quite a toss (single-core compiling gcc for at least one hour, time-consuming and energy-consuming) .

This article describes how to install a higher version of gcc from source.

Red Hat has actually compiled a higher version of gcc, but it has not updated to the two commonly used sources of base and epel, but puts these versions in scl.

2.1.1 Install gcc9

First install scl:

#yum -y install centos-release-scl

If you have used commands such as grouplist/install before, you should know that gcc is included in the Development Tools group. The gcc/g++ packages in scl are all prefixed with devtoolset.

The corresponding relationship between devtoolset and gcc version in scl is as follows:

devtoolset-3: gcc 4.9
devtoolset-4: gcc 5
devtoolset-6: gcc 6
devtoolset-7: gcc 7
devtoolset-8: gcc 8

Now version 9 is also available, install devtoolset-9 here

#yum -y install devtoolset-9

On RHEL, enable RHSCL repository for you system:

#yum-config-manager --enable rhel-server-rhscl-9-rpms
#scl enable devtoolset-9 bash

It is worth noting that these packages can be installed at the same time, will not overwrite and conflict with each other, and will not overwrite the version of the system. That is, multiple versions such as gcc 6, gcc 7, and gcc 8 can exist in the system at the same time.

Because the default gcc of the system will not be overwritten, there are four ways to use these software:

  1. Use an absolute path;
  2. Add the executable file path to the PATH environment variable;
  3. Use the officially recommended loading command: scl enable devtoolset-x bash, x is the version to be enabled;
  4. Execute the script that comes with the installation software: source /opt/rh/devtoolset-x/enable, where x is the version to be enabled.

In practice, the last two methods are recommended. For example, enable gcc 6: source /opt/rh/devtoolset-6/enable, then enter gcc -v to check that the version has changed to gcc 6.3.1. If you want to use a higher version for a long time, you can write this command into configuration files such as .bashrc.

It should be noted that the enablement of the scl command is only temporary, and the original system gcc version will be restored after exiting the shell or restarting.
If you want to use gcc 9.3 for a long time:

#echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
#source /etc/profile

After the upgrade is complete, be sure to run: gcc –version, g++ –version to see if the version number changes to the upgraded version of gcc and g++ before the upgrade is successful.

Although there is no lower version of GCC initially, when installing devtoolset-9, GCC4.8.5 will still be installed as a dependency in directories such as /usr/bin.

3 MySQL installation

MariaDB or some of its components are installed by default in CentOS. This is a branch of MySQL, but for the sake of need, you still need to install MySQL in the system.

3.1 Download and install MySQL's official Yum Repository

3.1.1 Installation

Uninstall MariaDB (query its installation software and uninstall them one by one, because of internal dependencies, you need to use rpm to ignore dependencies and force uninstall)

#rpm -qa | grep mariadb
#rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
#wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

Use the above command to directly download the Yum Repository for installation, and then you can install it directly with yum.

(There are more mysql-community.repo and mysql-community-source.repo in the /etc/yum.repos.d directory)

#yum -y install mysql57-community-release-el7-10.noarch.rpm

安装mysql mysql-community-server

#yum -y install mysql-community-server

Install mysql-community-devel

#yum install mysql-community-devel.x86_64

Because the Yum Repository is installed, every yum operation will be automatically updated in the future, and this needs to be uninstalled

#yum -y remove mysql57-community-release-el7-10.noarch

Rpm queries the installed mysql package:

[root@localhost g5]# rpm -qa | grep mysql
mysql-community-libs-5.7.35-1.el7.x86_64
mysql-community-server-5.7.35-1.el7.x86_64
mysql-community-common-5.7.35-1.el7.x86_64
mysql-community-devel-5.7.35-1.el7.x86_64
mysql-community-client-5.7.35-1.el7.x86_64

3.1.2 Settings

start mysql

# systemctl start  mysqld.service

Check running status

# systemctl status mysqld.service

To find out the initial password of the root user at this time, you can find the password in the log file by running the following command:

# grep "password" /var/log/mysqld.log

Enter the database:

# mysql -uroot -p

Enter initial password

By default, MySQL must change the password to operate the database:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';

Error:

ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
The reason is that MySQL has password setting specifications, specifically related to the value of validate_password_policy:

insert image description here

mysql> set global validate_password_policy=LOW;
mysql> set global validate_password_length=1;
mysql> set global validate_password_check_user_name=OFF;

Set the password again:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';

success!

3.1.3 Remote login settings

3.1.3.1 Root user login database

Authorize any IP to log in as root user

> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;

Overload grant table:

> flush privileges;

3.1.3.2 Open port 3306

# firewall-cmd --zone=public --add-port=3306/tcp --permanent
# firewall-cmd --reload
# firewall-cmd --list-ports

4 Qt

The installation of Qt in the Linux system is a little more complicated, because there are many Linux distributions, so the installation process is somewhat different. Since the Linux system can install the GNU tool set by itself (corresponding to MinGW in the Windows system), the Qt installation package in the Linux system does not come with g++ and other compilation tools and OpenGL development libraries, which need to be installed in advance according to different Linux distributions. Good GNU toolset and OpenGL development library. The Qt official website has a page introducing installation under Linux:

http://doc.qt.io/qt-5/linux.html

download link

https://download.qt.io/archive/qt/5.13/5.13.1/。

4.1 Preparations before installation

For Linux systems, the official Qt installation package always assumes that the C++ compiler, debugger, generator make, and other development tools are provided by the operating system itself. In addition, the OpenGL library file (extension: .a and .so) and header files (extension .h), the OpenGL development library also needs to be provided by the operating system itself. Therefore, before installing the official Qt run file, you must first install the GNU development tool set and OpenGL development library in the Linux system's own software source. The following installs the corresponding GNU development tool set and OpenGL development library for the three types of Linux operating systems. Please note that root authority is required to install the software, and the installation must be networked.

(1) If it is a Debian/Ubuntu/Deepin (apt-get) series operating system, execute the command:

#sudo apt-get install build-essential libgl1-mesa-dev

(2) If it is a Fedora/RHEL/CentOS (yum) series operating system, execute the command:

#yum groupinstall "C Development Tools and Libraries"
#yum install mesa-libGL-devel

Although it is the official method recommended by Qt, yum groupinstall "C Development Tools and Libraries" reported an error that it could not be found, so it was not installed. Although yum groupinstall "Development Tools" can be downloaded and installed, the version looks too old. It’s not as good as the previous devtoolset, so don’t download and install it, and finally just execute yum install mesa-libGL-devel.

(3) If it is an openSUSE (zypper) series operating system, execute the command:

#sudo zypper install -t pattern devel_basis

Execute corresponding commands according to different operating systems. After the installation is complete, you can continue the following installation.

XShell uploads qt-opensource-linux-x64-5.13.1.run to CentOS and grants execution permission

#chmod +x qt-opensource-linux-x64-5.13.1.run

4.2 Installation

In the graphical interface, double-click qt-opensource-linux-x64-5.13.1.run to install graphically (you need to log in to the Qt account), and the default directory (/opt/Qt5.13.1) is used for the installation directory. All components except Android Choose, install.

When the Qt development environment is installed, a Qt Creator shortcut file will be created, and then it will be displayed in the Linux start menu. Open the "Start Menu" –> "Applications" –> "Development" IDE of choice for Qt development.", you can see the Qt icon.

After clicking this tool, you can start Qt development. The shortcut file corresponding to this icon is located in: user home folder /.local/share/applications/, (.local is a hidden folder, which cannot be seen by default, press the shortcut key Ctrl + . in the KDE file manager, that is, Ctrl and Dot, for the GNOME file manager, press the shortcut key Ctrl + H to switch whether to display hidden files) The shortcut file name is DigiaQt-qtcreator-community.desktop (the path is /root/.local/share/applications).

For the convenience of using Qt Creator, you can right-click to copy the file, and then paste it into the desktop folder or main folder for use. Clicking the shortcut file will open the Qt Creator tool.

4.2.1 ldconfig configuration

Under the directory /etc/ld.so.conf.d/, create a new qt5.13.1-x86_64.conf with the content:

/opt/Qt5.13.1/5.13.1/gcc_64/lib
/opt/Qt5.13.1/Tools/QtCreator/lib

Execute ldconfig to refresh the dynamic library cache (refresh through the ld.so.conf file).

#ldconfig

look at the effect

#ldconfig -p

4.2.2 PATH environment variable configuration

It is better to configure the environment variables of the currently logged-in user (if you change the user, you will not be able to find the installed Qt).
NOTE: Environment variables can be simply divided into user-defined environment variables and system-level environment variables.

  1. User-level environment variable definition files: ~/.bashrc, ~/.profile (some systems: ~/.bash_profile)
  2. System-level environment variable definition files: /etc/bashrc, /etc/profile (some systems are: /etc/bash_profile), /etc/environment

In addition, in the user environment variables, the system will first read the ~/.bash_profile (or ~/.profile) file, if there is no such file, it will read ~/.bash_login, and then read ~/.bashrc according to the contents of these files .

The order in which Linux loads environment variables is as follows:

  1. /etc/environment

  2. /etc/profile

  3. /etc/bash.bashrc

  4. /etc/profile.d/test.sh

  5. ~/.profile

  6. ~/.bashrc

     #vim ~/.bashrc
    

Add at the end of ~/.bashrc (login and non-login shells can work):

export PATH=/opt/Qt5.13.1/5.13.1/gcc_64/bin:/opt/Qt5.13.1/Tools/QtCreator/bin:$PATH
export LD_LIBRARY_PATH=/opt/Qt5.13.1/5.13.1/gcc_64/lib:$LD_LIBRARY_PATH
export QT_PLUGIN_PATH=/opt/Qt5.13.1/5.13.1/gcc_64/plugins:$QT_PLUGIN_PATH
export QML2_IMPORT_PATH=/opt/Qt5.13.1/5.13.1/gcc_64/qml:$QML2_IMPORT_PATH

take effect

#source ~/.bash_profile

5 Qt MySQL driver

5.1 Open the MySQL driver project in the Qt source code

Qt Creator opens the mysql.pro project of /opt/Qt5.13.1/Src/qtbase/src/plugins/sqldrivers/mysql;

5.2 Project configuration

Project -> Build Settings, choose not to check Shadow build in both Debug and Release modes;

5.3 Modify the mysql.pro file

TARGET = qsqlmysql

HEADERS += $$PWD/qsql_mysql_p.h
SOURCES += $$PWD/qsql_mysql.cpp $$PWD/main.cpp

#QMAKE_USE += mysql#注释

OTHER_FILES += mysql.json

PLUGIN_CLASS_NAME = QMYSQLDriverPlugin

#添加
LIBS += /usr/lib64/mysql/libmysqlclient.so
INCLUDEPATH += /usr/include/mysql

include(../qsqldriverbase.pri)

5.4 Relying on the upper level qsqldriverbase.pri modification

QT  = core core-private sql-private

# For QMAKE_USE in the parent projects.
#include($$shadowed($$PWD)/qtsqldrivers-config.pri) #注释

PLUGIN_TYPE = sqldrivers
load(qt_plugin)

DEFINES += QT_NO_CAST_TO_ASCII QT_NO_CAST_FROM_ASCII

5.5 Compiler configuration

Tools->Kits->Build Kit->Compiler C, C++ are switched to the /opt directory
insert image description here

5.6 Release build

Project->build Set Release mode, execute qmake->build, success!

5.7 Debug build

Project->build set Debug mode, modify the first line of mysql.pro TARGET = qsqlmysqld, execute qmake->build, success!

5.8 View generated driver library

The driver library file is generated under the directory /opt/Qt5.13.1/5.13.1/Src/qtbase/src/plugins/sqldrivers/plugins/sqldrivers.

	[root@localhost sqldrivers]# ll
	总用量 2216
	-rwxr-xr-x. 1 root root 157304 8月  24 14:29 libqsqlmysqld.so
	-rw-r--r--. 1 root root 974304 8月  24 14:29 libqsqlmysqld.so.debug
	-rwxr-xr-x. 1 root root 157304 8月  24 14:24 libqsqlmysql.so
	-rw-r--r--. 1 root root 974304 8月  24 14:24 libqsqlmysql.so.debug

5.9 Copy

Copy these 4 files to /opt/Qt5.13.1/5.13.1/gcc_64/plugins/sqldrivers.

5.10 Attention

If GCC and G++ are not upgraded, an error will be reported g++: error: unrecognized command line option '-Wdate-time' (MySQL requires a higher version of GCC and G++).

6 Packaging method of Qt development under Linux

There are currently 3 methods available:

6.1 linuxdeployqt

GitHub provides a tool similar to windeployqt, but the function is weak (use https://github.com/probonopd/linuxdeployqt/releases Release build (7) Release or source code compilation, combined with patchself tool https://nixos.org/ releases/patchelf/patchelf-0.9/patchelf-0.9.tar.gz, you can start packaging).

6.1.1 Installation configuration

#sudo mv linuxdeployqt-continuous-x86_64.AppImage linuxdeployqt
#sudo mv ./linuxdeployqt /usr/local/bin
#chmod 777 linuxdeployqt
#sudo linuxdelpoyqt --version
	linuxdeployqt 6 (commit d41e234), build 724 built on 2020-07-31 15:51:47 UTC

Displaying the version indicates that the installation was successful.

Enter the patchelf decompressed folder and execute the following command

#./configure
#make
#sudo make install

Enter the /usr/local/bin directory and execute the ls command to display

linuxdeployqt  patchelf

Indicates that the configuration is successful.

6.1.2 General process of program packaging

Put the executable file of the program to be packaged into the folder where the executable file is located,
then enter the directory and execute the following command

#linuxdeployqt MysqlConnectionTest –appimage

It looks like it will report some mistakes, but it doesn't seem to matter.

The AppRun in the directory can start the program, success!

For scenarios that depend on third-party dynamic libraries in addition to Qt libraries and plug-ins, additional steps are required to package successfully. Proceed as follows:

  1. Use lddconfig to query the soft link of the executable file, and the one that shows "not found" is the required dynamic library/soft link of the dynamic library

  2. For those that need dynamic library soft links, manually copy the third-party library files (actual files, not soft links) to the packaging directory, and create soft links one by one. The command is as follows

     #ln -s dllfile dllfilelink
    
  3. linuxdeployqt does not take the current executable directory as part of LD_LIBRARY_PATH by default, so the command line is required to add the current directory to LD_LIBRARY_PATH

     #export LD_LIBRARY_PATH=/home/gggg/:$LD_LIBRARY_PATH
    
  4. Call the linuxdeployqt packaging command

     #linuxdeployqt MysqlConnectionTest –appimage
    
  5. At this time, the desktop shortcut file default.desktop, the default icon default.png, the program soft link AppRun, the dynamic library directory lib, and the Qt plug-in directory plugins are generated. All third-party dynamic libraries in the packaging directory need to be cut to the lib directory.

  6. The program can be started by invoking AppRun from the command line or by double-clicking the mouse.

In addition to manual solutions, there is also a script method to solve the problem of finding and copying third-party dynamic libraries, as follows:

  1. Create a script copylib.sh in the working directory of the project, the content is as follows (the content does not need to be modified)

     #!/bin/sh
     bin=$1         #发布的程序名称
    
     desDir="./lib" #lib文件夹  
    
     if [ ! -d $desDir ];then
     	#echo "makedir $desDir"
     	mkdir $desDir
     fi 
     libList=$(ldd $bin | awk  '{if (match($3,"/")){ printf("%s "),$3 } }')
     cp $libList $desDir
    
  2. After granting permission, the command runs

     chmod a+x ./copylib.sh
     ./copylib.sh ./myapp
    
  3. You can copy the required library from the lib directory to the lib directory under the packaging directory.

6.1.3 Attention

Using linuxdeployqt will change the executable, if you are concerned, don't use it.

6.2 linuxdeploy

linuxdeploy claims to be derived from linuxdeployqt, but it is more strict and easy to use, and provides a variety of input and output plug-in mechanisms. However, except for official information, there are almost no developers who actually use it, so it is not used for the time being.

front page

https://github.com/linuxdeploy/linuxdeploy

Plug-in mechanism

https://github.com/linuxdeploy/awesome-linuxdeploy#linuxdeploy-plugins

plug-in system

https://github.com/linuxdeploy/linuxdeploy/wiki/Plugin-system#plugin-discovery

Qt plugin

https://github.com/linuxdeploy/linuxdeploy-plugin-qt

user guide

https://docs.appimage.org/packaging-guide/from-source/linuxdeploy-user-guide.html

Native binary packaging

https://docs.appimage.org/packaging-guide/from-source/native-binaries.html

6.3 Script mode

The script method directly relies on manual judgment, and lacks the packaging of the Qt plug-in library, but it will not change the executable file. Proceed as follows:

  1. Copy the executable file to the packaging directory

  2. Create a new packaging script file (for example, named AppDeploy.sh) in the packaging directory, the content is as follows

     #!/bin/sh
     exe="MysqlConnectionTest"#存放你的可执行文件的名字
     des="/home/gggg/MysqlConnectionTest/"#可执行文件的路径
     deplist=$(ldd $exe | awk '{if(match($3,"/")){printf("%s "),$3}}')
     cp $deplist $des
    
  3. Give execution permissions to the packaging script

     #chmod +x AppDeploy.sh
    
  4. Execute the packaging script

     #./AppDeploy.sh
    
  5. This script can find the first-level system library that the executable file depends on and the library added to the environment variable (including the Qt library), and automatically copy it to the packaging directory, but for some third-party libraries (and libraries generated in the solution) , it is not processed and needs to be checked

     #ldd MysqlConnectionTest
    
  6. Each dependent library will be listed. For those whose arrow points to empty ("not found"), they need to be manually copied. For multi-level soft links, all levels of soft links need to be copied. Use the ll command to check whether the soft links are invalid. If If it fails, the soft link needs to be rebuilt. After completion, repeat step 5 until all dependent libraries can be found.

  7. Since Linux does not recognize the same-level directory of the executable file as part of the library directory by default, the executable file must be started through a script, and a .sh script file with the same name as the executable file is created in the packaging directory (note: the same name is required!), the content is as follows (Official recommendation, the content does not need to be changed):

     #!/bin/sh
     appname=`basename $0 | sed s,\.sh$,,`
    
     dirname=`dirname $0`
     tmp="${dirname#?}"
    
     if [ "${dirname%$tmp}" != "/" ]; then
     dirname=$PWD/$dirname
     fi
     LD_LIBRARY_PATH=$dirname
     export LD_LIBRARY_PATH
     $dirname/$appname "$@"
    
  8. grant execute permission

     #chmod +x 可执行文件名.sh
    
  9. The program can be started by starting the script

     #./可执行文件名.sh
    
  10. Copy the packaging directory to other hosts with the same operating system and start it directly with the startup script.

  11. There are other forms of startup scripts, as shown below (the name of the executable file in the script can be written correctly, and the name of the script file is not mandatory)

     #!/bin/bash
     export LD_LIBRARY_PATH=/app/lib:$LD_LIBRARY_PATH
     export QT_PLUGIN_PATH=/app/plugins:$QT_PLUGIN_PATH
     export QML2_IMPORT_PATH=/app/qml:$QML2_IMPORT_PATH
     ./MysqlConnectionTest
    

6.4 Additional application icon configuration

For default.desktop in the packaging directory, if you need to modify it:

Name值为可执行文件名,
Exec为可执行文件的绝对路径名,
Icon为图标的绝对路径,
Terminal为终端启动开关

Copy default.desktop after renaming

全局安装(所有用户可用),将xxx.desktop 复制到/usr/share/applications
当前用户可用, 将xxx.desktop 复制到 ~/.local/share/applications
也可拷贝到桌面作为桌面快捷方式

7 References

【1】CentOS7 yum way to install MySQL5.7. https://www.cnblogs.com/porotin/p/9045391.html

[2] Compile Qt's mysql driver under Centos7. http://www.voidcn.com/article/p-cirrynjy-tm.html

【3】 Install Qt 5.9.2 on Linux CentOS7. https://www.cnblogs.com/lsgxeva/p/7688884.html

【4】 Qt for Linux/X11. https://doc.qt.io/qt-5/linux.html

【5】Building Qt environment under linux. https://blog.csdn.net/baidu_24256693/article/details/48198889

【6】Linux environment variable configuration strategy. https://www.cnblogs.com/niuben/p/14049694.html

【7】Upgrade gcc to higher versions such as 4.8, 4.9, 5.2, 6.3, 7.3 for CentOS 6 and 7. https://www.vpser.net/manage/centos-6-upgrade-gcc.html

【8】 Supported Platforms.https://doc.qt.io/archives/qt-5.13/ supported-platforms.html

【9】 centos upgrade gcc. https://www.cnblogs.com/dream397/p/14148796.html

【10】 Developer Toolset 8 by Software Collections. https://www.softwarecollections.org/en/scls/rhscl/devtoolset-8/

[11] centos7 installs gcc7.3.0 with devtoolset. https://www.jianshu.com/p/046deadb5cca

[12] How to uninstall some applications (Qt, and other applications) that come with Centos. https://blog.csdn.net/NEUQ_zxy/article/details/77434684

Packaging method
[13] centos7 linuxdeployqt qt5.13.1 packaging program. https://blog.csdn.net/hu626626/article/details/100918455

【14】 https://github.com/probonopd/linuxdeployqt/releases

[15] Ultra-detailed QT under Linux uses appimage packaging program. https://www.pianshen.com/article/912312212/

[16] QT packaging method under linux. https://www.cnblogs.com/xupeidong/p/9395193.html

【17】Qt official packaging method. https://doc.qt.io/qt-5/linux-deployment.html

[18] Package release of Qt program under Linux. https://blog.csdn.net/hjl_1991/article/details/50365927

[19] Package and publish qt program under Linux (using linuxdelpoyqt, shell script). https://blog.csdn.net/u014746574/article/details/79288727

Guess you like

Origin blog.csdn.net/langshuibaren/article/details/119971279