Build Sqlite Project with Cmake
-
I am attempting to build a project using
QSqlTableModel
andQSqlDatabase
usingcmake
instead ofqmake
.
When I try to create aQSqlDatabase
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 theQt6Sql
directory as follows:
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
? -
You are mixing importing methods.
- revert the latest changes you made,
- replace
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
withfind_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets Sql REQUIRED)
- replace
target_link_libraries(SqliteTableViewer PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
withtarget_link_libraries(SqliteTableViewer PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Sql)
P.S.
Theset(CMAKE
would be better placed as target properties.
Instead ofset(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 )
-
@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?
-
You are mixing importing methods.
- revert the latest changes you made,
- replace
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
withfind_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets Sql REQUIRED)
- replace
target_link_libraries(SqliteTableViewer PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
withtarget_link_libraries(SqliteTableViewer PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Sql)
P.S.
Theset(CMAKE
would be better placed as target properties.
Instead ofset(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 )