赞
踩
系统架构:arm64架构 + intel 架构
Qt版本:6.2.2 (安装时务必勾选源码)
MySQL版本: 8.0.29
可选方式 :
export PATH=${PATH}:/usr/local/mysql/bin
brew install mysql
等待安装完成后验证mysql版本
mysql --version
使用where命令查询mysql的位置
where mysql
使用which命令查询当前使用的mysql可执行程序位置
which mysql
homebrew需使用的库的位置如下
/opt/homebrew/Cellar/mysql/8.0.29/lib/libmysqlclient.dylib
通过homebrew安装
使用如下命令
homebrew install cmake
开启新的终端
以始上述环境变量生效
终端创建编译目录
mkdir build-sqldrivers
cd build-sqldrivers
mkdir build
cd build
cmake -G "Unix Makefiles" \
/Users/xxx/Qt6.2.2/6.2.2/Src/qtbase/src/plugins/sqldrivers \
-DCMAKE_PREFIX_PATH=/Users/xxx/Qt/Qt6.2.2/6.2.2/macos \
-DCMAKE_INSTALL_PREFIX=../ \
-DMySQL_INCLUDE_DIR="/usr/local/mysql/include" \
-DMySQL_LIBRARY="/usr/local/mysql/lib/libmysqlclient.dylib"
正常识别成功如图所示
3. 执行如下命令以进行编译
若为m1
还需修改编译的架构,此问题的产生原因是是qt源码错误导致
/xxxxx/build-sqldrivers/build/mysql/CMakeFiles/QMYSQLDriverPlugin.dir/link.txt
将x86_64改为 arm64 后重新编译
cmake --build .
此时两个架构均能编译成功
cmake --install .
此时目录结构如图所示
5. 拷贝lib/cmake/Qt6Sql
中的关于MySQL的cmkae
识别文件至/Users/xxxx/Qt6.2.2/6.2.2/macos/lib/cmake/Qt6Sql
6.拷贝plugins/sqldrivers/libqsqlmysql.dylib
动态库文件至/Users/xxxx/Qt6.2.2/6.2.2/macos/plugins/sqldrivers
新建一个cmake工程
cmkaelist如下代码所示
注意加入Sql在如下代码中
find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets Sql REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets Sql
target_link_libraries(untitled PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Sql)
完整cmakelist.txt
cmake_minimum_required(VERSION 3.5) project(untitled VERSION 0.1 LANGUAGES CXX) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets Sql REQUIRED) find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets Sql REQUIRED) set(PROJECT_SOURCES main.cpp widget.cpp widget.h widget.ui ) if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) qt_add_executable(untitled MANUAL_FINALIZATION ${PROJECT_SOURCES} ) # Define target properties for Android with Qt 6 as: # set_property(TARGET untitled APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR # ${CMAKE_CURRENT_SOURCE_DIR}/android) # For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation else() if(ANDROID) add_library(untitled SHARED ${PROJECT_SOURCES} ) # Define properties for Android with Qt 5 after find_package() calls as: # set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android") else() add_executable(untitled ${PROJECT_SOURCES} ) endif() endif() target_link_libraries(untitled PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Sql) set_target_properties(untitled PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} ) if(QT_VERSION_MAJOR EQUAL 6) qt_finalize_executable(untitled) endif()
在main函数替换如下代码进行测试
#include <QApplication>
#include <QSqlDatabase>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qDebug()<<"available drivers:";
QStringList drivers = QSqlDatabase::drivers();
foreach(QString driver, drivers)
qDebug()<<driver;
return a.exec();
}
运行结果如图所示,已支持QMYSQL
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。