一、 本文使用系统环境
系统架构:arm64架构 + intel 架构 Qt版本:6.2.2 (安装时务必勾选源码) MySQL版本: 8.0.29
二、下载MySQL
可选方式 :
- 官网下载对应dmg,根据不同架构选择不同的版本
安装pkg即可,在最后需设置环境变量
export PATH=${PATH}:/usr/local/mysql/bin
- homebrew 安装 ,homebrew配置教程见该文章
终端执行如下代码
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
三、安装cmake
通过homebrew安装 使用如下命令
homebrew install cmake
四、编译mysql
-
开启新的终端 以始上述环境变量生效 -
终端创建编译目录
mkdir build-sqldrivers
cd build-sqldrivers
mkdir build
cd build
- 继续执行cmake识别命令 注意,请根据以下指令填入你的qt源码路径、qt识别路径、mysql include 目录 mysql lib目录,
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. 执行如下命令以进行编译
cmake --build .
此时intel下能正常编译成功,m1编译失败(待补充解决方案,本人已解决) 4. 执行安装命令
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
五、测试mysql
新建一个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
|