Undefined reference to QSqlDatabase and other QtSql headers of Qt CMake project on Linux
-
wrote on 1 Nov 2024, 13:24 last edited by
In my Qt C++/QML project, I included <QtSql/QSqlDatabase>, <QtSql/QSqlError>, <QtSql/QSqlRelationalTableModel>, etc. headers. In CMakeLists.txt I write
find_package(Qt6 6.8 REQUIRED COMPONENTS Charts Core Gui Qml Quick Sql)
and
target_link_libraries(MyApp PRIVATE Qt6::Charts Qt6::Core Qt6::Gui Qt6::Qml Qt6::Quick Qt6::Sql QXlsx::QXlsx )
The project can be successfully built and ran on Windows platform, but it fails to build and run on Linux platform:
x86_64-linux-gnu/libOpenGL.so /opt/Qt6.8.0/6.8.0/gcc_64/lib/libQt6Qml.so.6.8.0 /opt/Qt6.8.0/6.8.0/gcc_64/lib/libQt6Network.so.6.8.0 /opt/Qt6.8.0/6.8.0/gcc_64/lib/libQt6Core.so.6.8.0 -Wl,-rpath-link,/opt/Qt6.8.0/6.8.0/gcc_64/lib && :
/usr/bin/ld: content/libcontent.a(SqlTreeModel.cpp.o): in functionSqlTreeModel::SqlTreeModel(QObject*)': /home/<username>/MyApp/src/sql/SqlTreeModel.cpp:12:(.text+0x17b): undefined reference to
QSqlDatabase::drivers()'
/usr/bin/ld: content/libcontent.a(SqlTreeModel.cpp.o): in functionSqlTreeModel::addConnection(QString const&, QString const&, QString const&, QString const&)': /home/<username>/MyApp/src/sql/SqlTreeModel.cpp:182:(.text+0xeeb): undefined reference to
QSqlError::QSqlError(QString const&, QString const&, QSqlError::ErrorType, QString const&)'
/usr/bin/ld: /home/<username>/MyApp/src/sql/SqlTreeModel.cpp:209:(.text+0x10b4): undefined reference to `QSqlDatabase::addDatabase(QString const&, QString const&)'and many lines of other similar errors.
I checked CMake configuration that Qt6Sql_DIR is /opt/Qt6.8.0/6.8.0/gcc_64/lib/cmake/Qt6Sql which exists in my filesystem. I've also built SQL driver plugins on Linux.
What's the possible reason for these errors? -
Hi,
If you are creating a library, you have to link the dependencies which you are not doing here.
-
Please post the full CMakeLists.txt
-
wrote on 2 Nov 2024, 11:00 last edited by
cmake_minimum_required(VERSION 3.27) option(LINK_INSIGHT "Link Qt Insight Tracker library" ON) option(BUILD_QDS_COMPONENTS "Build design studio components" ON) project(SuisApp VERSION 1.0.0 DESCRIPTION "Open-source Solar Cell Simulator" HOMEPAGE_URL "https://github.com/yihuajack/Suis" LANGUAGES CXX) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Qt6 6.8 REQUIRED COMPONENTS Charts Core Gui Qml Quick Sql) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTORCC ON) find_package(Boost 1.83.0 REQUIRED) include_directories(${Boost_INCLUDE_DIRS}) include_directories(src) # for content/CMakeLists.txt qt6_add_qml_module SOURCES include_directories(src/material) include_directories(src/sql) if (Qt6_VERSION VERSION_GREATER_EQUAL 6.8) qt_standard_project_setup(REQUIRES 6.8) endif() qt_add_executable(SuisApp WIN32 MACOSX_BUNDLE) qt_add_resources(SuisApp "configuration" PREFIX "/" FILES qtquickcontrols2.conf ) add_subdirectory(src) include_directories(${ORACLE_INCLUDE_DIR}) include(FetchContent) FetchContent_Declare( QXlsx GIT_REPOSITORY https://github.com/QtExcel/QXlsx.git GIT_TAG v1.4.8 SOURCE_SUBDIR QXlsx ) FetchContent_MakeAvailable(QXlsx) set_target_properties(SuisApp PROPERTIES MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) target_link_libraries(SuisApp PRIVATE Qt6::Charts Qt6::Core Qt6::Gui Qt6::Qml Qt6::Quick Qt6::Sql QXlsx::QXlsx ) include(GNUInstallDirs) install(TARGETS SuisApp BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) if (BUILD_QDS_COMPONENTS) include(${CMAKE_CURRENT_SOURCE_DIR}/qmlcomponents) endif() include(${CMAKE_CURRENT_SOURCE_DIR}/qmlmodules) if (LINK_INSIGHT) include(${CMAKE_CURRENT_SOURCE_DIR}/insight) endif () set(QML_IMPORT_PATH ${PROJECT_BINARY_DIR}/qml CACHE PATH "Path to the custom QML components defined by the project") qt_generate_deploy_qml_app_script( TARGET SuisApp OUTPUT_SCRIPT deploy_script MACOS_BUNDLE_POST_BUILD NO_UNSUPPORTED_PLATFORM_ERROR DEPLOY_USER_QML_MODULES_ON_UNSUPPORTED_PLATFORM ) install(SCRIPT ${deploy_script})
src/CMakeLists.txt:
target_sources(SuisApp PRIVATE core/BuildDevice.tpp core/Device.h core/DistFun.h core/GetVarSub.h core/ParameterClass.h # material headers material/DbSysModel.h material/IniConfigParser.h material/MaterialDbModel.h material/OpticMaterial.h material/ParameterSystem.h # material sources material/DbSysModel.cpp material/IniConfigParser.cpp material/MaterialDbModel.cpp material/OpticMaterial.cpp material/ParameterSystem.cpp # optics headers optics/FixedMatrix.h optics/OpticStack.h optics/tmm.h optics/TransferMatrix.h # optics sources optics/FixedMatrix.cpp optics/OpticStack.cpp optics/tmm.cpp optics/tmm_vec.cpp # sql headers sql/SqlTreeItem.h # sql sources sql/SqlTreeItem.cpp # utils headers utils/Approx.h utils/Fs.h utils/Log.h utils/Math.h utils/Range.h # utils sources utils/Approx.cpp utils/Fs.cpp utils/Log.cpp utils/Math.cpp utils/Range.cpp # top headers Application.h CommandLineParseResult.h Global.h Parameter.h Preferences.h Profile.h ProfilePrivate.h SettingsStorage.h # top sources Application.cpp CommandLineParseResult.cpp Parameter.cpp Preferences.cpp Profile.cpp ProfilePrivate.cpp SettingsStorage.cpp main.cpp )
content/CMakeLists.txt:
qt_add_library(content STATIC) qt6_add_qml_module(content URI "content" VERSION 1.0 RESOURCE_PREFIX "/qt/qml" # qt_policy(SET QTP0001 NEW) QML_FILES App.qml BandDiagramDialog.qml DbLoginDialog.qml DeviceTableDialog.qml ElectricalParsetPage.qml ElectricalParsetPageForm.ui.qml OpticalDeviceDialog.qml OpticalMaterialDialog.qml # loader qml files cannot be omitted OpticalParsetPage.qml OpticalParsetPageForm.ui.qml ResultPage.qml ResultPageForm.ui.qml SimPage.qml Splash.qml SqlBrowserWindow.qml WelcomePage.qml WelcomePageForm.ui.qml WizardFlow.qml WizardFlowForm.ui.qml RESOURCES fonts/fonts.txt images/SuisLogo.svg images/arrow_icon.png SOURCES ../src/Process.h ../src/Process.cpp ../src/material/DeviceModel.h ../src/material/DeviceModel.cpp ../src/material/DevSysModel.h ../src/material/DevSysModel.cpp # sql headers ../src/sql/SqlTreeModel.h # sql sources ../src/sql/SqlTreeModel.cpp )
-
Hi,
If you are creating a library, you have to link the dependencies which you are not doing here.
-
Hi,
If you are creating a library, you have to link the dependencies which you are not doing here.
-
wrote on 2 Nov 2024, 19:18 last edited by
It seems that the problem could be solved by appending one line
target_link_libraries(content PUBLIC Qt6::Sql)
However, it remains mysterious that on Windows this line is not necessary. Besides, why does only Qt6::Sql need to be explicitly linked but Qt6::Core doesn't?
-
No, in your
content
project you have to link the Qt dependencies you are using. -
1/8