配置开发环境
编译器GCC >= 4.9或者Visual Studio >= 2012,开发工具Perl >= 5.12
编译QtWebEngine需要Python >= 2.7.5
QDoc从QT 5.11开始使用Clang解析C++代码,构建QDoc文档需要Clang
使用MinGW静态编译
使用在上篇文章中MinGW静态编译的OPENSSL和MariaDB,并把生成的内容分别复制到D:\Qt\LIB\openssl和D:\Qt\LIB\mysql下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 set PATH=D:\Qt\Qt5.12.5\Tools\mingw730_64\bin;D:\Qt\DevTools\Python27;D:\Qt\DevTools\perl\perl\bin;%PATH%set LLVM_INSTALL_DIR=D:\Qt\DevTools\LLVM-9.0.0-win64 cd D:\Qt\qt-everywhere-src-5.12.5\mkdir build & cd build..\Configure.bat -confirm-license -opensource -platform win32-g++ -debug-and-release -static -prefix "D:\Qt\Qt5.12.5_MinGW_Static" MYSQL_INCDIR="D:\Qt\LIB\mysql\include\mariadb" MYSQL_LIBDIR="D:\Qt\LIB\mysql\lib" -L "D:\Qt\LIB\openssl\lib" MYSQL_LIBS="-lmariadbclient -lssl -lcrypto -lws2_32 -lgdi32 -luser32 -lwsock32 -ladvapi32 -lcrypt32" OPENSSL_INCDIR="D:\Qt\LIB\openssl\include" OPENSSL_LIBDIR="D:\Qt\LIB\openssl\lib" OPENSSL_LIBS="-lssl -lcrypto -lws2_32 -lgdi32 -ladvapi32 -lcrypt32 -luser32 -lz" -qt-sqlite -sql-mysql -openssl-linked -qt-zlib -qt-libpng -qt-libjpeg -opengl desktop -qt-freetype -qt-pcre -qt-harfbuzz -no-qml-debug -no-angle -nomake tests -nomake examples -skip qtwebengine -skip wayland ..\Configure.bat -confirm-license -opensource -platform win32-g++ -debug-and-release -static -prefix "D:\Qt\Qt5.12.5_MinGW_Static" \ MYSQL_INCDIR="D:\Qt\LIB\mysql\include\mariadb" MYSQL_LIBDIR="D:\Qt\LIB\mysql\lib" -L "D:\Qt\LIB\openssl\lib" \ MYSQL_LIBS="-lmariadbclient -lssl -lcrypto -lws2_32 -lgdi32 -luser32 -lwsock32 -ladvapi32 -lcrypt32" \ OPENSSL_INCDIR="D:\Qt\LIB\openssl\include" OPENSSL_LIBDIR="D:\Qt\LIB\openssl\lib" \ OPENSSL_LIBS="-lssl -lcrypto -lws2_32 -lgdi32 -ladvapi32 -lcrypt32 -luser32 -lz" \ -qt-sqlite -sql-mysql -openssl-linked -qt-zlib -qt-libpng -qt-libjpeg -opengl desktop -qt-freetype -qt-pcre -qt-harfbuzz \ -no-qml-debug -no-angle -nomake tests -nomake examples -skip qtwebengine -skip wayland mingw32-make -j12 mingw32-make install
使用MSVC静态编译
OpenSSL静态库 | MYSQL ,下载这两个静态库,并把里面的内容分别复制到D:\Qt\VS14\openssl\和D:\Qt\VS14\mysql下
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 cd C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat amd64 set PATH=D:\Qt\DevTools\Python27;D:\Qt\DevTools\perl\perl\bin;%PATH%set LLVM_INSTALL_DIR=D:\Qt\DevTools\LLVM-9.0.0-win64 cd D:\Qt\qt-everywhere-src-5.12.5\mkdir build & cd build..\Configure.bat -confirm-license -opensource -platform win32-msvc -mp -debug-and-release -static -prefix "D:\Qt\Qt5.12.5_VS14_Static" MYSQL_INCDIR="D:\Qt\VS14\mysql\include" MYSQL_LIBDIR="D:\Qt\VS14\mysql\lib" MYSQL_LIBS="-lmysqlclient -lGdi32 -lAdvapi32 -lUser32 -lCrypt32" OPENSSL_INCDIR="D:\Qt\VS14\openssl\include" OPENSSL_LIBDIR="D:\Qt\VS14\openssl\lib" OPENSSL_LIBS="-llibssl_static -llibcrypto_static -lws2_32 -lGdi32 -lAdvapi32 -lUser32 -lCrypt32" -qt-sqlite -sql-mysql -openssl-linked -qt-zlib -qt-libpng -qt-libjpeg -opengl desktop -qt-freetype -qt-pcre -qt-harfbuzz -no-qml-debug -no-angle -nomake tests -nomake examples -skip qtwebengine -skip wayland ..\Configure.bat -confirm-license -opensource -platform win32-msvc -mp -debug-and-release -static -prefix "D:\Qt\Qt5.12.5_VS14_Static" \ MYSQL_INCDIR="D:\Qt\VS14\mysql\include" MYSQL_LIBDIR="D:\Qt\VS14\mysql\lib\vs14" \ MYSQL_LIBS="-lmysqlclient -lGdi32 -lAdvapi32 -lUser32 -lCrypt32" \ OPENSSL_INCDIR="D:\Qt\VS14\openssl\include" OPENSSL_LIBDIR="D:\Qt\VS14\openssl\lib" \ OPENSSL_LIBS="-llibssl_static -llibcrypto_static -lws2_32 -lGdi32 -lAdvapi32 -lUser32 -lCrypt32" \ -qt-sqlite -sql-mysql -openssl-linked -qt-zlib -qt-libpng -qt-libjpeg -opengl desktop -qt-freetype -qt-pcre -qt-harfbuzz \ -no-qml-debug -no-angle -nomake tests -nomake examples -skip qtwebengine -skip wayland nmake nmake install
其他问题
一定要链接路径D:\Qt{LIB|VS14}\mysql\lib和D:\Qt{LIB|VS14}\openssl\lib里面的静态库,链接了动态库会报错
注意32位的编译器不能用64位的库,否则会报错。-sql-mysql代表直接链接,-plugin-sql-mysql作为插件构建
添加Qt库时报qmlscene未安装的警告,复制动态库中的bin/qmlscene.exe和相关DLL到静态库的bin文件夹中即可
在Qt Creator中使用MinGW静态编译的QT库,Visual Studio中使用请自行搜索
1 2 添加Qt版本:工具 -> 选项 -> 构件和运行(Kits) -> Qt Versions -> 添加 -> D:\Qt\Qt5.12.5_MinGW_Static\bin\qmake.exe -> 版本名称:Qt %{Qt:Version} (Qt5.12.5_MinGW_Static) -> Apply 设置到套件:工具 -> 选项 -> 构件和运行(Kits) -> 构建套件(Kits) -> 添加 -> 名称:Desktop Qt %{Qt:Version} MinGW 64-bit Static -> Qt Version:Qt5.12.5 (Qt5.12.5_MinGW_Static) -> Apply
如果软件使用MinGW静态编译后如果还是需要 libgcc_s_xxx.dll 等库,将如下文件修改或添加两行参数即可解决:
1 2 3 4 5 6 QMAKE_LFLAGS = -static QMAKE_LFLAGS_DLL = -static
如果软件使用MSVC静态编译后如还是显示需要 xxx.dll 等库,则将如下文件修改或添加两行参数即可解决:
1 2 3 4 5 6 7 QMAKE_CFLAGS_RELEASE = $$QMAKE_CFLAGS_OPTIMIZE -MT QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += $$QMAKE_CFLAGS_OPTIMIZE -Zi -MT QMAKE_CFLAGS_DEBUG = -Zi -MTd
测试 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 #include <QCoreApplication> #include <QDebug> #include <QSqlDatabase> int main (int argc, char *argv[]) { QCoreApplication a (argc, argv) ; qDebug() << QSqlDatabase::drivers(); QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL" ); db.setHostName("HostName" ); db.setDatabaseName("DatabaseName" ); db.setUserName("UserName" ); db.setPassword("Password" ); db.setPort(3306 ); if (db.open()) qDebug() << "连接数据库成功" ; else qDebug() << "连接数据库失败" ; return a.exec(); }