开发环境
因为我Debian系统中没有安装QT,所以在Debian下交叉编译相关库后拷贝到Win系统下使用对应版本的qmake编译mysql模块 我这里编译的是Arm64架构的库,交叉编译库所使用的编译工具为android-ndk-r14b,高版本交叉编译时会出错 因为MySQL怎么编译都通不过所以选择了MariaDB,而MariaDB库依赖libiconv和openssl,所以它们也需要编译
1 2 3 4 5 6 7 export ANDROID_NDK_HOME="/opt/android-ndk-r14b" BIN_PATH="$ANDROID_NDK_HOME /toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin" SR="$ANDROID_NDK_HOME /platforms/android-24/arch-arm64" BR="$BIN_PATH /aarch64-linux-android-" export PATH="$BIN_PATH " :$PATH
编译libiconv库 1 2 3 4 5 6 7 8 9 10 11 wget https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz tar -xzvf libiconv-1.14.tar.gz cd libiconv-1.14mkdir build & cd buildSTRIP="$BR " strip RANLIB="$BR " ranlib OBJDUMP="$BR " objdump AR="$BR " ar \ CC="$BR " gcc CPP="$BR " cpp CFLAGS=--sysroot=$SR CPPFLAGS=$CFLAGS \ ../configure --host=arm --prefix=/home/leux/libiconv --with-sysroot=$SR --enable-static make & make install
编译OPENSSL库 1 2 3 4 5 6 7 8 9 10 11 wget https://www.openssl.org/source/openssl-1.1.1d.tar.gz tar -xzvf openssl-1.1.1d.tar.gz cd openssl-1.1.1dmkdir build & cd build../Configure android-arm64 --prefix=/home/leux/openssl no-shared \ zlib --with-zlib-lib="$SR /usr/lib/libz.a" --with-zlib-include="$SR /usr/include" ANDROID_DEV=$SR /usr make make install_sw
编译MariaDB库 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 wget http://mirrors.nav.ro/mariadb//connector-c-2.3.7/mariadb-connector-c-2.3.7-src.tar.gz tar -xzvf mariadb-connector-c-2.3.7-src.tar.gz cd mariadb-connector-c-2.3.7-srcmkdir build & cd buildsed -i -e "s|ADD_SUBDIRECTORY(unittest/libmariadb)|#ADD_SUBDIRECTORY(unittest/libmariadb)|" ../CMakeLists.txt sed -i -e "N; s|#define _global_h|#define _global_h\n\n#ifndef ushort\n#define ushort uint16\n#endif|" ../include/my_global.h sed -i -e "N; s|SET_TARGET_PROPERTIES(libmariadb PROPERTIES VERSION.*||" ../libmariadb/CMakeLists.txt sed -i -e "N; s|SOVERSION \${CPACK_PACKAGE_VERSION_MAJOR})||" ../libmariadb/CMakeLists.txt sed -i -e "N; s|\${CPACK_PACKAGE_VERSION_MAJOR}||" ../libmariadb/CMakeLists.txt PKG_CONFIG_PATH=$SR /usr/lib/pkgconfig cmake .. \ -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_C_FLAGS=--sysroot="$SR " \ -DCMAKE_INSTALL_PREFIX="/home/leux/mariadb" \ -DCMAKE_C_COMPILER="$BR " gcc \ -DCMAKE_LINKER="$BR " ld \ -DCMAKE_AR="$BR " ar \ -DCMAKE_NM="$BR " nm \ -DCMAKE_OBJCOPY="$BR " objcopy \ -DCMAKE_OBJDUMP="$BR " objdump \ -DCMAKE_RANLIB="$BR " ranlib \ -DCMAKE_STRIP="$BR " strip \ -DWITH_OPENSSL=ON -DOPENSSL_ROOT_DIR="/home/leux/openssl" \ -DOPENSSL_CRYPTO_LIBRARY="/home/leux/openssl/lib/libcrypto.a" \ -DOPENSSL_SSL_LIBRARY="/home/leux/openssl/lib/libssl.a" \ -DICONV_INCLUDE_DIR="/home/leux/libiconv/include" \ -DICONV_LIBRARIES="/home/leux/libiconv/lib/libiconv.a" \ -DWITH_EXTERNAL_ZLIB=ON \ -DZLIB_INCLUDE_DIR="$SR /usr/include" \ -DZLIB_LIBRARY="$SR /usr/lib/libz.a" sed -i -e "N; s|#define HAVE_UCONTEXT_H 1|/* #undef HAVE_UCONTEXT_H */|" include/my_config.h sed -i -e "N; s|#define HAVE_GETPWNAM 1|/* #undef HAVE_GETPWNAM */|" include/my_config.h sed -i -e "N; s|#define HAVE_STPCPY 1|/* #undef HAVE_STPCPY */|" include/my_config.h make & make install
Linux下编译QT模块 1 2 3 4 5 6 7 8 9 10 11 export ANDROID_NDK_ROOT="$ANDROID_NDK_HOME " qmake="/opt/Qt/Qt5.12.5/5.12.5/android_arm64_v8a/bin/qmake" cd /opt/Qt/qtbase-everywhere-src-5.12.5/src/plugins/sqldrivers/mysql/$qmake -o Makefile mysql.pro "INCLUDEPATH+=$Install_Dir /mariadb/include/mariadb" \"LIBS+=-L$Install_Dir /libiconv/lib -L$Install_Dir /openssl/lib -L$Install_Dir /mariadb/lib/mariadb -liconv -lssl -lcrypto -lmariadbclient" make & make install
Win32下编译QT模块 1 2 3 4 5 6 7 8 9 10 set ANDROID_NDK_ROOT=D:\Qt\Android-SDK\android-ndk-r20set PATH=D:\Qt\Qt5.12.5\5.12.5\android_arm64_v8a\bin;D:\Qt\Android-SDK\android-ndk-r20\prebuilt\windows-x86_64\bin;%PATH% cd D:\Qt\qtbase-everywhere-src-5.12.5\src\plugins\sqldrivers\mysql\qmake "INCLUDEPATH+=-ID:\Qt\Arm64\mariadb\include\mariadb" "LIBS+=-LD:\Qt\Arm64\libiconv\lib -LD:\Qt\Arm64\openssl\lib -LD:\Qt\Arm64\mariadb\lib\mariadb -lmariadbclient -liconv -lssl -lcrypto" -o Makefile mysql.pro make & make install
问题解决
如果qmake报错找不到:qtsqldrivers-config.pri,则新建它,内容如下两行:
1 2 3 4 5 QMAKE_LIBS_MYSQL = D:\Qt\Arm64\mariadb\lib\mariadb\libmariadbclient.a QMAKE_INCDIR_MYSQL = D:\Qt\Arm64\mariadb\include\mariadb
安装完成后会在下面路径看到多了两个文件
1 2 3 D:\Qt\Qt5.12.5\5.12.5\android_arm64_v8a\plugins\sqldrivers\libqsqlmysql.so D:\Qt\Qt5.12.5\5.12.5\android_arm64_v8a\lib\cmake\Qt5Sql\Qt5Sql_QMYSQLDriverPlugin.cmake