Qt6 desktop app and shared library with cmake - c++

I am trying to make a project with subdirectories using cmake.
When using qmake this mechanism was built in Qt creator.
The project contains a shared library and a desktop app.
Below is the structure of the project and cmake I have:
Main cmake:
cmake_minimum_required (VERSION 3.5)
project(contractapp)
add_subdirectory(contract-core)
add_subdirectory(contract-desktop)
Desktop app cmake:
project(contract-desktop LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
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)
find_package(contract-core REQUIRED)
set(PROJECT_SOURCES
main.cpp
MainWindow.cpp
MainWindow.h
MainWindow.ui
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(contract-desktop
${PROJECT_SOURCES}
)
else()
if(ANDROID)
add_library(contract-desktop SHARED
${PROJECT_SOURCES}
)
else()
add_executable(contract-desktop
${PROJECT_SOURCES}
)
endif()
endif()
target_link_libraries(contract-desktop PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
Shared library cmake:
cmake_minimum_required(VERSION 3.14)
project(contract-core LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED)
add_library(contract-core SHARED
contract-core_global.h
Person.cpp
Person.h
)
target_link_libraries(contract-core PRIVATE Qt${QT_VERSION_MAJOR}::Core)
target_compile_definitions(contract-core PRIVATE CONTRACTCORE_LIBRARY)
My problem is that the desktop application does not see the library that I am making.

That library has to be copied to the same folder the application is in. Otherwise, each is in its own folder, and the application won't see it. Most likely you'd want to set the target folder for both binaries (the application and the library) to be some common build subfolder of both.

Related

How can I add QtCharts library to my project?

I was trying to add QtCharts library to my project but an error occurred:
Target "PTP3" links to target "Qt6::Charts" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing?
I installed Qt5::Charts and Qt6::Charts via Qt MaintenanceTool but the issue remained. Anyone knows how to fix it?
I'm using Qt Creator 7.0.2. Here is my Cmake file. I followed the instructions from https://doc.qt.io/qt-6/qtcharts-index.html but it didn't help.:
cmake_minimum_required(VERSION 3.5)
project(PTP3 VERSION 0.1 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)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets Charts REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
set(PROJECT_SOURCES
main.cpp
footballmatches.cpp
footballmatches.h
footballmatches.ui
pivottable.h
pivottable.cpp
pivottable.ui
stadiumschart.h
stadiumschart.cpp
stadiumschart.ui
some_qml.qml
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(PTP3
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
# Define target properties for Android with Qt 6 as:
# set_property(TARGET PTP3 APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
# ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
if(ANDROID)
add_library(PTP3 SHARED
${PROJECT_SOURCES}
)
# Define properties for Android with Qt 5 after find_package() calls as:
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
else()
add_executable(PTP3
${PROJECT_SOURCES}
)
endif()
endif()
target_link_libraries(PTP3 PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
target_link_libraries(PTP3 PRIVATE Qt${QT_VERSION_MAJOR}::Charts)
set_target_properties(PTP3 PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(PTP3)
endif()
you should have both these lines in your Cmake File for adding one of the qt libs to your project for example for qt charts you need these:
find_package(Qt5Charts REQUIRED)
target_link_libraries(${PROJECT_NAME}
Qt5::Charts
)
Be sure to add target_link_libraries at the end of your CMakeLists.txt file.

QT6 Insists on using cdb instead of gdb

I am using Qt 6.0.4 in Win10, the 'Help/About' says QtCreator version is 4.15.0. I have a very simple desktop application, built using CMake, and using QCustomPlot that crashes. I am using the Kit Qt 6.0.4 mingw81_64. When I look into the settings under manage kits, I see the debugger is supposedly set to 'GNU gdb 8.1 for MinGW 8.1.0 64Bit'. But when I start debugging, I get a message box that seems to indicate it is using cdb, not gdb. In the Debuggers tab for the kit, it shows all the GNU gdb debuggers, but also to CDB, all auto-detected. I can not remove the cdb entries, at least not that I can figure out. How do I turn off cdb so it only uses the gdb that seems to be called for in the kit settings??
My CMakeLists.txt file is:
project(QT60DataAcq VERSION 0.1 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
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 COMPONENTS Widgets PrintSupport REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets PrintSupport REQUIRED)
include_directories(C:/Pgms/Qt/QCUSTOMPLOT2X)
set(PROJECT_SOURCES
main.cpp
dacqmain.cpp
dacqmain.h
dacqmain.ui
C:/Pgms/Qt/QCUSTOMPLOT2X/qcustomplot.cpp
)
set(CMAKE_BUILD_TYPE Debug)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin/)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(QT60DataAcq
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
else()
if(ANDROID)
add_library(QT60DataAcq SHARED
${PROJECT_SOURCES}
)
else()
add_executable(QT60DataAcq
${PROJECT_SOURCES}
)
endif()
endif()
target_link_libraries(QT60DataAcq PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::PrintSupport)
set_target_properties(QT60DataAcq
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin/
LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib/
RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin/
)
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(QT60DataAcq)
endif()

Cannot use Qt Charts - QT6 | Cmake

I am using Qt 6.2.1 with cmake and I can't find a way to use Qt Charts.
The Qt Charts is installed under "C:\Qt\6.2.1\Src\qtcharts"
In my cmakelists.txt I've included the Qt Charts in find_package and target_link_libraries. That seemed to work on qt5 (found on other answers in StackOverflow) but don't work on qt6. (I've hidden the project sources files)
cmake_minimum_required(VERSION 3.5)
project(projectname VERSION 0.1 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)
set(CMAKE_CXX_FLAGS_DEBUG "/MDd")
find_package(QT NAMES Qt6 COMPONENTS Widgets Charts REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Xml Widgets REQUIRED)
set(PROJECT_SOURCES
...
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(projectname
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
else()
add_executable(projectname
${PROJECT_SOURCES}
)
endif()
target_link_libraries(projectname PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Xml Qt6::Charts)
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(projectname)
endif()
I get this error:
C:\Qt\6.2.1\msvc2019_64\lib\cmake\Qt6Core\Qt6CoreMacros.cmake:559: error: Target "projectname" links to target "Qt6::Charts" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? C:/Qt/6.2.1/msvc2019_64/lib/cmake/Qt6Core/Qt6CoreMacros.cmake:523 (_qt_internal_create_executable) C:/Qt/6.2.1/msvc2019_64/lib/cmake/Qt6Core/Qt6CoreMacros.cmake:847 (qt6_add_executable) CMakeLists.txt:78 (qt_add_executable)
SOLUTION (Thanks to #vre):
I had to select the Qt Charts under 6.2.1 and not under Qt in the online installer
Qt Online Installer

Why VS doesn't see included files?

I am a student writing a project. I have created a program (here's the link to SVN: https://mysvn.ru/Ilya_Antonov/antonov_sem_prj). I use CMake to build it and generate the .sln project. But when run it in Visual Studio, there are some errors: the compiler can't open the files "drumswindow.h" and "mainwindow.h" (fatal error C1083). Please, take a look at my CMakeLists.txt:
cmake_minimum_required(VERSION 3.1.0)
project(MusicSimulator VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
if(CMAKE_VERSION VERSION_LESS "3.7.0")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()
find_package(Qt5 COMPONENTS Gui REQUIRED)
find_package(Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt5 COMPONENTS Core REQUIRED)
find_package(Qt5 COMPONENTS Multimedia REQUIRED)
add_executable(MusicSimulator
mainwindow.ui
mainwindow.cpp
mainwindow.h
main.cpp
drumswindow.ui
drumswindow.cpp
drumswindow.h
keyswindow.ui
keyswindow.cpp
keyswindow.h
images.qrc
drumres.qrc
keysres2.qrc
keysres3.qrc
keysres4.qrc
keysres5.qrc
keysres6.qrc
)
target_link_libraries(MusicSimulator Qt5::Widgets Qt5::Gui Qt5::Core Qt5::Multimedia)
What have I done wrong here?
You don't include the current source dir and therefore the compiler doesn't find it. Either add the current source dir or use the correct include statements (#include "keyswindow.h" for example)

QColorSpace: No such file or directory

I'm trying to run the imageviewer example from this website https://doc.qt.io/qt-5/qtwidgets-widgets-imageviewer-example.html.
However, for some reason I don't seem to have QColorSpace.
I tried to include all of the possible libraries I thinnk I would need but still have no luck.
Is it my CMake file or do I need to somehow install QColorSpace?
cmake_minimum_required(VERSION 3.5)
project(SAT_Solver 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 http://doc.qt.io/qt-5/deployment-android.html for more information.
# They need to be set before the find_package(Qt5 ...) call.
#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 PrintSupport OpenGL Core Gui REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets PrintSupport OpenGL Core Gui REQUIRED)
if(ANDROID)
add_library(SAT_Solver SHARED
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
ScribbleArea.cpp
ScribbleArea.h
)
else()
add_executable(SAT_Solver
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
ScribbleArea.cpp
ScribbleArea.h
imageviewer.h
imageviewer.cpp
)
endif()
#qt5_add_resources(RC_SRC "resources/qml.qrc")
#qt5_add_resources(RC_SRC "/home/nalfredo/School/AutoSoftware/Project/QT/SAT_Solver/Resources.qrc")
target_link_libraries(SAT_Solver PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::PrintSupport
${QT_QTSVG_LIBRARY} Qt${QT_VERSION_MAJOR}::OpenGL Qt${QT_VERSION_MAJOR}::Gui Qt${QT_VERSION_MAJOR}::Core Qt5::Gui)