Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Undefined reference to QSqlDatabase and other QtSql headers of Qt CMake project on Linux
Forum Updated to NodeBB v4.3 + New Features

Undefined reference to QSqlDatabase and other QtSql headers of Qt CMake project on Linux

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 683 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yihua Liu
    wrote on 1 Nov 2024, 13:24 last edited by
    #1

    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 function SqlTreeModel::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 function SqlTreeModel::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?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 2 Nov 2024, 11:39 last edited by
      #4

      Hi,

      If you are creating a library, you have to link the dependencies which you are not doing here.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Y 1 Reply Last reply 2 Nov 2024, 18:46
      1
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 1 Nov 2024, 15:01 last edited by
        #2

        Please post the full CMakeLists.txt

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • Y Offline
          Y Offline
          Yihua Liu
          wrote on 2 Nov 2024, 11:00 last edited by
          #3
          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
          )
          
          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 2 Nov 2024, 11:39 last edited by
            #4

            Hi,

            If you are creating a library, you have to link the dependencies which you are not doing here.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            Y 1 Reply Last reply 2 Nov 2024, 18:46
            1
            • S SGaist
              2 Nov 2024, 11:39

              Hi,

              If you are creating a library, you have to link the dependencies which you are not doing here.

              Y Offline
              Y Offline
              Yihua Liu
              wrote on 2 Nov 2024, 18:46 last edited by
              #5

              @SGaist Hi,

              It seems that I've already linked the library in qmlmodules:

              add_subdirectory(content)
              add_subdirectory(imports)
              
              target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE
                  contentplugin
                  Suisplugin
              )
              

              Do you mean this?

              1 Reply Last reply
              0
              • Y Offline
                Y Offline
                Yihua Liu
                wrote on 2 Nov 2024, 19:18 last edited by
                #6

                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?

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 2 Nov 2024, 19:18 last edited by
                  #7

                  No, in your content project you have to link the Qt dependencies you are using.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  Y 1 Reply Last reply 3 Nov 2024, 01:53
                  0
                  • S SGaist
                    2 Nov 2024, 19:18

                    No, in your content project you have to link the Qt dependencies you are using.

                    Y Offline
                    Y Offline
                    Yihua Liu
                    wrote on 3 Nov 2024, 01:53 last edited by
                    #8

                    @SGaist Thank you for your reminder anyway. Closing this thread

                    1 Reply Last reply
                    0
                    • Y Yihua Liu has marked this topic as solved on 3 Nov 2024, 01:54

                    1/8

                    1 Nov 2024, 13:24

                    • Login

                    • Login or register to search.
                    1 out of 8
                    • First post
                      1/8
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved