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. Build Sqlite Project with Cmake
Forum Update on Monday, May 27th 2025

Build Sqlite Project with Cmake

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 3.3k Views
  • 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.
  • T Offline
    T Offline
    trozzel
    wrote on 6 May 2021, 13:39 last edited by trozzel 5 Jun 2021, 13:46
    #1

    I am attempting to build a project using QSqlTableModel and QSqlDatabase using cmake instead of qmake.
    When I try to create a QSqlDatabase member, upon building, I receive the following:

    C:/Users/*****/Documents/Cpp/GTDProject/SqliteTableViewer/mainwindow.cpp:6: undefined reference to `__imp__ZN12QSqlDatabaseC1Ev'
    C:/Users/*****/Documents/Cpp/GTDProject/SqliteTableViewer/mainwindow.cpp:6: undefined reference to `__imp__ZN12QSqlDatabaseD1Ev'
    CMakeFiles/SqliteTableViewer.dir/mainwindow.cpp.obj: In function `MainWindow::~MainWindow()':
    C:/Users/*****/Documents/Cpp/GTDProject/SqliteTableViewer/mainwindow.cpp:18: undefined reference to `__imp__ZN12QSqlDatabaseD1Ev'
    

    In the Projects, Build Settings page, there is a list of Key: Value pairs in which the key is an arbitrary tag and the value is a path to a directory containing cmake files for linking and perhaps building Qt libraries. I added the path to the Qt6Sql directory as follows:
    SqliteLibrary.PNG

    I should probably remove the last highlighted item, as it is a directory containing DLL's.

    I am pretty sure that if I had built this project using qmake instead, adding the macro, QT += sql would take care of this issue.

    Does anyone know how to link against the appropriate library(s) using cmake?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 6 May 2021, 16:50 last edited by VRonin 5 Jun 2021, 16:55
      #4

      You are mixing importing methods.

      1. revert the latest changes you made,
      2. replace find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED) with find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets Sql REQUIRED)
      3. replace target_link_libraries(SqliteTableViewer PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) with target_link_libraries(SqliteTableViewer PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Sql)

      P.S.
      The set(CMAKE would be better placed as target properties.
      Instead of

      set(CMAKE_AUTOUIC ON)
      set(CMAKE_AUTOMOC ON)
      set(CMAKE_AUTORCC ON)
      set(CMAKE_CXX_STANDARD 11)
      set(CMAKE_CXX_STANDARD_REQUIRED ON)
      

      It should be

      set_target_properties(SqliteTableViewer PROPERTIES
              AUTOMOC ON
              AUTOUIC ON
              AUTORCC  ON
              CXX_STANDARD 11
              CXX_STANDARD_REQUIRED ON
      )
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • V Offline
        V Offline
        VRonin
        wrote on 6 May 2021, 16:08 last edited by
        #2

        Can you show us your CMakeLists.txt?
        Without it it's difficult to help you.
        My guess would be you forgot find_package(Qt COMPONENTS Sql REQUIRED) / target_link_libraries(mytarget PRIVATE Qt::Sql)

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        0
        • T Offline
          T Offline
          trozzel
          wrote on 6 May 2021, 16:43 last edited by trozzel 5 Jun 2021, 16:43
          #3

          @VRonin said in Build Sqlite Project with Cmake:

          You were correct. I didn't know if Qt handled it some other way on that project setup page.
          So, I added those two lines as you suggested and am getting another error (see after CMakeLists.txt file.

          cmake_minimum_required(VERSION 3.5)
          
          project(SqliteTableViewer 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)
          
          # QtCreator supports the following variables for Android, which are identical to qmake Android variables.
          # Check https://doc.qt.io/qt/deployment-android.html for more information.
          # They need to be set before the find_package( ...) calls below.
          
          #if(ANDROID)
          #    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
          #    if (ANDROID_ABI STREQUAL "armeabi-v7a")
          #        set(ANDROID_EXTRA_LIBS
          #            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
          #            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
          #    endif()
          #endif()
          
          find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
          find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
          
          set(PROJECT_SOURCES
                  main.cpp
                  mainwindow.cpp
                  mainwindow.hpp
                  mainwindow.ui
          )
          
          if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
              qt_add_executable(SqliteTableViewer
                  ${PROJECT_SOURCES}
              )
          
          else()
              if(ANDROID)
                  add_library(SqliteTableViewer SHARED
                      ${PROJECT_SOURCES}
                  )
              else()
                  add_executable(SqliteTableViewer
                      ${PROJECT_SOURCES}
                  )
              endif()
          endif()
          
          find_package(Qt COMPONENTS Sql REQUIRED)
          target_link_libraries(SqliteTableViewer PRIVATE Qt${QT_VERSION_MAJOR}::Widgets
            PRIVATE Qt::Sql)
          

          Here is the new error:

          CMake Error at C:/Qt/6.0.3/mingw81_64/lib/cmake/Qt6Core/Qt6CoreMacros.cmake:470 (add_executable):
            Target "SqliteTableViewer" links to target "Qt::QSql" but the target was
            not found.  Perhaps a find_package() call is missing for an IMPORTED
            target, or an ALIAS target is missing?
          
          1 Reply Last reply
          0
          • V Offline
            V Offline
            VRonin
            wrote on 6 May 2021, 16:50 last edited by VRonin 5 Jun 2021, 16:55
            #4

            You are mixing importing methods.

            1. revert the latest changes you made,
            2. replace find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED) with find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets Sql REQUIRED)
            3. replace target_link_libraries(SqliteTableViewer PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) with target_link_libraries(SqliteTableViewer PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Sql)

            P.S.
            The set(CMAKE would be better placed as target properties.
            Instead of

            set(CMAKE_AUTOUIC ON)
            set(CMAKE_AUTOMOC ON)
            set(CMAKE_AUTORCC ON)
            set(CMAKE_CXX_STANDARD 11)
            set(CMAKE_CXX_STANDARD_REQUIRED ON)
            

            It should be

            set_target_properties(SqliteTableViewer PROPERTIES
                    AUTOMOC ON
                    AUTOUIC ON
                    AUTORCC  ON
                    CXX_STANDARD 11
                    CXX_STANDARD_REQUIRED ON
            )
            

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            2
            • T Offline
              T Offline
              trozzel
              wrote on 6 May 2021, 17:02 last edited by
              #5

              @VRonin said in Build Sqlite Project with Cmake:

              That did the trick. Thank you VERY much.

              1 Reply Last reply
              0

              1/5

              6 May 2021, 13:39

              • Login

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