QPluginLoader Failed to load qsqlmysql.dll
-
Hi, I'm using Qt 6.5 cmake to build my program. From the beginning till now, I have tried several solutions given in Qt Forum to load MySQL. Below I have mentioned a solution is runnable but convert to exe is not runnable. I have also attached qsqlmysql.dll method below. Please read till below. Anyone helps is appreciated.
I tried by setting up the path to MySQL Server 8.0 directly and it works. For those that are wondering how did I import MySQL Server 8.0 into my project below is the code.
cmakelist.txt set(MYSQL_DIR "C:/Program Files/MySQL/MySQL Server 8.0") set(MYSQL_INCLUDE_DIR "${MYSQL_DIR}/include") find_library(MYSQL_LIBRARY NAMES mysql PATHS ${MYSQL_DIR}/lib ) target_link_libraries(target ${MYSQL_LIBRARY} )
Currently I want to deploy my project into exe and it seems that by setting up the path to MySQL Server 8.0 would not work.
For the second solution, I built qsqlmysql.dll into mingw_64 and copied into my project build. I tried to load the qsqlmysql.dll in my main.cpp but I received and error.
Failed to load the plugin: "Cannot load library D:\\qmlManualEdi\\build-ManualEDI-Desktop_Qt_6_5_2_MinGW_64_bit-Debug\\plugins\\sqldrivers\\qsqlmysql.dll: The specified module could not be found." QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: available drivers: QSQLITE QODBC QPSQL QMARIADB QMYSQL Failed to connect to MySQL database: "Driver not loaded Driver not loaded"
Attached is also the method on how I load my qsqlmysql.dll
main.cpp // Load MySQL driver QString currPath = QDir::currentPath(); currPath += "/plugins/sqldrivers"; QCoreApplication::addLibraryPath(currPath); QString mysqlDll = "qsqlmysql"; QPluginLoader pluginLoader(mysqlDll); if (pluginLoader.load()) { qDebug() << "Plugin loaded successfully!"; QObject *pluginInstance = pluginLoader.instance(); if (pluginInstance) { // Plugin-specific functionality can be accessed here } else { qDebug() << "Failed to create plugin instance."; } } else { qDebug() << "Failed to load the plugin:" << pluginLoader.errorString(); }
-
C Christian Ehrlicher moved this topic from QML and Qt Quick on
-
Here is a final version on how to solve this related matter.
- Ensure that you have set your system path for mysql.
SET PATH=%PATH%;[mysql/bin]
- In your cmakelist, link mysql library to project
set(MYSQL_DIR "C:/Program Files/MySQL/MySQL Server 8.0") set(MYSQL_INCLUDE_DIR "${MYSQL_DIR}/include") find_library(MYSQL_LIBRARY NAMES mysql PATHS ${MYSQL_DIR}/lib ) target_link_libraries(target ${MYSQL_LIBRARY} )
- If you like to deploy the project to other pc, please ensure that you have included necessary file needed for mysql.
For my case, the mysql version I'm using is MySQL Server 8.0. What I need to do was copy libcrypto-3-x64.dll, libssl-3-x64.dll and libmysql.dll to my project directory. Here a cmakelist version of copy the dlls needed.
set(MYSQL_REQUIRED_FILES "${MYSQL_DIR}/bin/libcrypto-3-x64.dll" "${MYSQL_DIR}/bin/libssl-3-x64.dll" "${MYSQL_DIR}/lib/libmysql.dll" ) foreach(SOURCE_FILE IN LISTS MYSQL_REQUIRED_FILES) get_filename_component(FILE_NAME ${SOURCE_FILE} NAME) add_custom_command (TARGET appManualEDI POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${SOURCE_FILE} ${CMAKE_BINARY_DIR}/${FILE_NAME} COMMENT "Copying ${FILE_NAME} to ${CMAKE_BINARY_DIR}" ) endforeach()
-
Hi and welcome to devnet,
Build time and runtime dependencies are not the same thing.
Your application has to find its dependencies at runtime either because they are present in the same folder as the executable or can be found through the PATH environment variable.
-
Hi and welcome to devnet,
Build time and runtime dependencies are not the same thing.
Your application has to find its dependencies at runtime either because they are present in the same folder as the executable or can be found through the PATH environment variable.
@SGaist Hi,
this is my cmakelist.txt. Is this match what you mentioned about runtime?set(QT_SQL_DRIVERS_SOURCE_DIR "D:/MMU/Degree/Intern/iRadar/sourceTree/qmlManualEdi/build-ManualEDI-Desktop_Qt_6_5_2_MinGW_64_bit-Debug/sqldrivers") set(QT_SQL_DRIVERS_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/plugins/sqldrivers") ... install(DIRECTORY "${QT_SQL_DRIVERS_SOURCE_DIR}/" DESTINATION "${QT_SQL_DRIVERS_INSTALL_DIR}" FILES_MATCHING PATTERN "*.dll" # Change the pattern if needed ) install(TARGETS appManualEDI BUNDLE DESTINATION . # CONFIGURATIONS Debug LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} )
-
@SGaist Hi,
this is my cmakelist.txt. Is this match what you mentioned about runtime?set(QT_SQL_DRIVERS_SOURCE_DIR "D:/MMU/Degree/Intern/iRadar/sourceTree/qmlManualEdi/build-ManualEDI-Desktop_Qt_6_5_2_MinGW_64_bit-Debug/sqldrivers") set(QT_SQL_DRIVERS_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/plugins/sqldrivers") ... install(DIRECTORY "${QT_SQL_DRIVERS_SOURCE_DIR}/" DESTINATION "${QT_SQL_DRIVERS_INSTALL_DIR}" FILES_MATCHING PATTERN "*.dll" # Change the pattern if needed ) install(TARGETS appManualEDI BUNDLE DESTINATION . # CONFIGURATIONS Debug LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} )
@JCherry The Qt Mysql plugin DLL nneeds to be deployed with your program, usually into a
/plugins/sqldriver
subdirectory. You seem to be doing this.At run time, your program will load the Qt Mysql plugin which needs to find the Mysql run time DLL(s), either in the same directory as your program executable or on the system's PATH. The Mysql run time DLL(s) are not part of Qt, they'll likely be somewhere under "C:/Program Files/MySQL/MySQL Server 8.0" on your development machine. It is up to you to deploy those DLLs with your program onto the target machine, or arrange for them to be present another way.
-
Latest Update
I seems to able to get the qsqlmysql.dll to loaded successfully. All I did was set the path to the plugins and use target_link_libraries. Please take note that I'm not sure how it works but it works.cmakelist.txt set(QT_SQL_DRIVERS_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/plugins/sqldrivers") target_link_libraries(appManualEDI ${MYSQL_LIBRARY} )
// Load MySQL driver QString currPath = QDir::currentPath(); currPath += "/plugins/sqldrivers"; QCoreApplication::addLibraryPath(currPath); QString mysqlDll = "qsqlmysql"; QPluginLoader pluginLoader(mysqlDll); if (pluginLoader.load()) { qDebug() << "Plugin loaded successfully!"; QObject *pluginInstance = pluginLoader.instance(); if (pluginInstance) { // Plugin-specific functionality can be accessed here } else { qDebug() << "Failed to create plugin instance."; } } else { qDebug() << "Failed to load the plugin:" << pluginLoader.errorString(); }
But when deploy to executable file it still does not work. My guess is that it won't load in executable file is because of my install part doesnt seem to install the qsqlmysql.dll. But I'm not sure how to do it.
-
@JCherry The Qt Mysql plugin DLL nneeds to be deployed with your program, usually into a
/plugins/sqldriver
subdirectory. You seem to be doing this.At run time, your program will load the Qt Mysql plugin which needs to find the Mysql run time DLL(s), either in the same directory as your program executable or on the system's PATH. The Mysql run time DLL(s) are not part of Qt, they'll likely be somewhere under "C:/Program Files/MySQL/MySQL Server 8.0" on your development machine. It is up to you to deploy those DLLs with your program onto the target machine, or arrange for them to be present another way.
-
Latest Update
I seems to able to get the qsqlmysql.dll to loaded successfully. All I did was set the path to the plugins and use target_link_libraries. Please take note that I'm not sure how it works but it works.cmakelist.txt set(QT_SQL_DRIVERS_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/plugins/sqldrivers") target_link_libraries(appManualEDI ${MYSQL_LIBRARY} )
// Load MySQL driver QString currPath = QDir::currentPath(); currPath += "/plugins/sqldrivers"; QCoreApplication::addLibraryPath(currPath); QString mysqlDll = "qsqlmysql"; QPluginLoader pluginLoader(mysqlDll); if (pluginLoader.load()) { qDebug() << "Plugin loaded successfully!"; QObject *pluginInstance = pluginLoader.instance(); if (pluginInstance) { // Plugin-specific functionality can be accessed here } else { qDebug() << "Failed to create plugin instance."; } } else { qDebug() << "Failed to load the plugin:" << pluginLoader.errorString(); }
But when deploy to executable file it still does not work. My guess is that it won't load in executable file is because of my install part doesnt seem to install the qsqlmysql.dll. But I'm not sure how to do it.
@JCherry Runtime environments is total different from build time.
Any path you set in the cmakelist.txt will only affect build time.
The reason you can run it in IDEs is because they are defaultly set to copy build time library search path to runtime PATH.
But when you double click the exe to run, the runtime PATH is not set to include that.
So you either set the runtime PATH (by 1. setting the system's PATH or 2.write a bat/cmd file, set PATH in it and then start the exe from that), or also copy Mysql dlls with your exe as @ChrisW67 mentioned. -
Here is a final version on how to solve this related matter.
- Ensure that you have set your system path for mysql.
SET PATH=%PATH%;[mysql/bin]
- In your cmakelist, link mysql library to project
set(MYSQL_DIR "C:/Program Files/MySQL/MySQL Server 8.0") set(MYSQL_INCLUDE_DIR "${MYSQL_DIR}/include") find_library(MYSQL_LIBRARY NAMES mysql PATHS ${MYSQL_DIR}/lib ) target_link_libraries(target ${MYSQL_LIBRARY} )
- If you like to deploy the project to other pc, please ensure that you have included necessary file needed for mysql.
For my case, the mysql version I'm using is MySQL Server 8.0. What I need to do was copy libcrypto-3-x64.dll, libssl-3-x64.dll and libmysql.dll to my project directory. Here a cmakelist version of copy the dlls needed.
set(MYSQL_REQUIRED_FILES "${MYSQL_DIR}/bin/libcrypto-3-x64.dll" "${MYSQL_DIR}/bin/libssl-3-x64.dll" "${MYSQL_DIR}/lib/libmysql.dll" ) foreach(SOURCE_FILE IN LISTS MYSQL_REQUIRED_FILES) get_filename_component(FILE_NAME ${SOURCE_FILE} NAME) add_custom_command (TARGET appManualEDI POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${SOURCE_FILE} ${CMAKE_BINARY_DIR}/${FILE_NAME} COMMENT "Copying ${FILE_NAME} to ${CMAKE_BINARY_DIR}" ) endforeach()
-