编译驱动插件参考如下:
Mac Qt 6.2.2 MySQL驱动_U3_Bono的博客-CSDN博客
我遇到的问题:执行$cmake --build . 时出现Failed
[1/1] Linking CXX shared module plugins/sqldrivers/libqsqlmysql.dylib
FAILED: plugins/sqldrivers/libqsqlmysql.dylib
: && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -g -DNDEBUG -O2 -arch x86_64 -isysroot /Users/XXX/Qt/6.2.4/macos/lib/QtCore.framework/Versions/A/QtCore -framework DiskArbitration -framework IOKit && cd /Users/XXX/build-sqldrivers/mysql && /Users/XXX/Qt/Tools/CMake/CMake.app/Contents/bin/cmake -E make_directory /Users/XXX/bsqldrivers/plugins/sqldrivers/libqsqlmysql.dylib.dSYM/Contents/Resources/DWARF/qsqlmysql
ld: warning: ignoring file /usr/local/mysql-8.0.30-macos12-arm64/lib/libmysqlclient.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
Undefined symbols for architecture x86_64:
可以看到,Mac的m1款其架构师arm64,而不是X86_64,但是在build的时候显示在X86_64架构下,因此我前去寻找办法,经过一系列探索得到:
编辑~/build-sqldrivers/build-ninja文件
vim build-ninja
修改build配置,把所有出现-arch x86_64的地方更改成 -arch arm64(记住是所有)
如下:
build mysql/CMakeFiles/QMYSQLDriverPlugin.dir/QMYSQLDriverPlugin_autogen/mocs_compilation.cpp.o: CXX_COMPILER__QMYSQLDriverPlugin_RelWithDebInfo /Users/XXX/build-sqldrivers/mysql/QMYSQLDriverPlugin_autogen/mocs_compilation.cpp || cmake_object_order_depends_target_QMYSQLDriverPlugin
DEFINES = -DGL_SILENCE_DEPRECATION -DQMYSQLDriverPlugin_EXPORTS -DQT_CORE_LIB -DQT_DEPRECATED_WARNINGS -DQT_DEPRECATED_WARNINGS_SINCE=0x060000 -DQT_DISABLE_DEPRECATED_BEFORE=0x050000 -DQT_NO_CAST_FROM_ASCII -DQT_NO_CAST_TO_ASCII -DQT_NO_DEBUG -DQT_NO_EXCEPTIONS -DQT_NO_JAVA_STYLE_ITERATORS -DQT_NO_NARROWING_CONVERSIONS_IN_CONNECT -DQT_PLUGIN -DQT_SQL_LIB -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE
DEP_FILE = mysql/CMakeFiles/QMYSQLDriverPlugin.dir/QMYSQLDriverPlugin_autogen/mocs_compilation.cpp.o.d
FLAGS = -g -DNDEBUG -O2 -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -mmacosx-version-min=12.0 -fPIC -fvisibility=hidden -fvisibility-inlines-hidden -Wall -Wextra -fno-exceptions -fapplication-extension -std=c++17
build mysql/CMakeFiles/QMYSQLDriverPlugin.dir/qsql_mysql.cpp.o: CXX_COMPILER__QMYSQLDriverPlugin_RelWithDebInfo /Users/XXX/Qt/6.2.4/Src/qtbase/src/plugins/sqldrivers/mysql/qsql_mysql.cpp || cmake_object_order_depends_target_QMYSQLDriverPlugin
DEFINES = -DGL_SILENCE_DEPRECATION -DQMYSQLDriverPlugin_EXPORTS -DQT_CORE_LIB -DQT_DEPRECATED_WARNINGS -DQT_DEPRECATED_WARNINGS_SINCE=0x060000 -DQT_DISABLE_DEPRECATED_BEFORE=0x050000 -DQT_NO_CAST_FROM_ASCII -DQT_NO_CAST_TO_ASCII -DQT_NO_DEBUG -DQT_NO_EXCEPTIONS -DQT_NO_JAVA_STYLE_ITERATORS -DQT_NO_NARROWING_CONVERSIONS_IN_CONNECT -DQT_PLUGIN -DQT_SQL_LIB -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE
DEP_FILE = mysql/CMakeFiles/QMYSQLDriverPlugin.dir/qsql_mysql.cpp.o.d
FLAGS = -g -DNDEBUG -O2 -arch arm64 -isysroot
# Link the shared module plugins/sqldrivers/libqsqlmysql.dylib
build plugins/sqldrivers/libqsqlmysql.dylib: CXX_MODULE_LIBRARY_LINKER__QMYSQLDriverPlugin_RelWithDebInfo mysql/CMakeFiles/QMYSQLDriverPlugin.dir/QMYSQLDriverPlugin_autogen/mocs_compilation.cpp.o mysql/CMakeFiles/QMYSQLDriverPlugin.dir/main.cpp.o mysql/CMakeFiles/QMYSQLDriverPlugin.dir/qsql_mysql.cpp.o | /usr/local/mysql-8.0.30-macos12-arm64/lib/libmysqlclient.dylib || mysql/QMYSQLDriverPlugin_autogen mysql/QMYSQLDriverPlugin_autogen_timestamp_deps
ARCH_FLAGS = -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -mmacosx-version-min=12.0
如果单独更改 Link the shared module plugins/sqldrivers/libqsqlmysql.dylib时的-arch arm64会出现:虽然在/Users/XXX/Qt/6.2.4/macos/plugins/sqldrivers编译出了mysql的插件libqsqlmysql.dylib,但是最后无法正确链接,出现Qt creater无法加载的情况!!!!