How to handle package dependencies into a Qt5 CMake project? - c++

I'm kinda stucked in a C++ Linux-based school project and i need some kind of guidance in Libraries and Packages. Please be patient since i'm newbie in these type of projects :')
I need to do a cross-platform installable game with some colleagues.
I developed a qt5 app that uses QWidget and QMultimedia. I had to install some packages to actually run the app (for example: qt5, qt5multimedia, gstreamer1.0 for audio, and probably more since i actually lost track of them). Since i need to merge this application into this school project that i'm talking about, my colleagues will have to install these packages as well (as everyone who will try to install the game, probably?).
We'll be using Cmake to build the project. This is my actual CMakeLists.txt, if anyone needs to take a look at it:
cmake_minimum_required(VERSION 3.5)
project(dune 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)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Multimedia REQUIRED)
set(PROJECT_SOURCES
main.cpp
vistas.h
mainwindow.cpp
mainwindow.ui
lobbywindow.cpp
lobbywindow.ui
resources.qrc
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(dune
${PROJECT_SOURCES}
)
else()
if(ANDROID)
add_library(dune SHARED
${PROJECT_SOURCES}
)
else()
add_executable(dune
${PROJECT_SOURCES}
)
endif()
endif()
target_link_libraries(dune PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Multimedia)
What i'm trying to get is a project that has all of these dependencies in a lib folder, but i've had a hard time looking for Qt libraries online. QtCreator allows you to create Shared Libraries but my installed version doesn't have QMultimedia on the options.
• What is the best way to track these dependencies?
• Is it actually needed to do this? Should i worry about it, or just find the packages that i installed and tell my colleagues to install them?
Please tell if i came short explaining my problem or i need to specify something, thanks!

Related

VS 2019 CMake looking for file it is not supposed to

I run into a problem when building a Cmake Project with CmakeLists.txt in VS 2019.
It tells me this:
What I did before the error:
I created a tests folder
I created a test.cpp file
A Pop up window opened from Cmake -> asking if I want to add it automatically to add_executable, I declined it
I manually added it to the Project Sources with tests/test.cpp
test got highlighted blue, so I changed the name to sample_test.cpp
rewrote test.cppp to sample_test.cpp
build CmakeLists.txt
error
I hope the provided information is enough otherwise let me know and thanks in advance!
What I tried:
deleting the cache
deleting the out file
deleting CMakeSettings.Json
repaired VS 2019
uninstalled CMake and reinstalled
Added the "ghost" file -> results in same error message
deleted repo and cloned it in a completely new directory
deleted .vs file
reinstalling VS 2019
deleting VS with InstallCleanup.exe, removed every VS file and reinstalled
Good to know:
On a colleagues machine it build without problems, so on his machine Cmake doesn't look for this file. So the problem seems to be locally on the machine.
Also I do not want this file and folder anymore, I have deleted it and somehow CMake is still looking for it.
cmake_minimum_required(VERSION 3.5)
project(team1 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)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
find_package(OpenCV REQUIRED)
find_package(PCL REQUIRED COMPONENTS common visualization io)
set(PROJECT_SOURCES
main.cpp
mainwindow.cpp mainwindow.h mainwindow.ui
userinterface.h userinterface.cpp
fileviewer.h fileviewer.cpp
segmentcreator.h segmentcreator.cpp
filerepresentation.h filerepresentation.cpp
filehandler.h filehandler.cpp
utils/utils.cpp utils/utils.h
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(${PROJECT_NAME}
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
else()
if(ANDROID)
add_library(${PROJECT_NAME} SHARED
${PROJECT_SOURCES}
)
# Define properties for Android with Qt 5 after find_package() calls as:
else()
add_executable(${PROJECT_NAME}
${PROJECT_SOURCES}
)
endif()
endif()
target_link_libraries(${PROJECT_NAME}
PRIVATE Qt${QT_VERSION_MAJOR}::Widgets
${OpenCV_LIBS}
${PCL_LIBRARIES}
)
set_target_properties(${PROJECT_NAME} 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
)
install(TARGETS ${PROJECT_NAME}
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
target_include_directories(${PROJECT_NAME} PUBLIC
${OpenCV_INCLUDE_DIRS}
)
target_link_directories(${PROJECT_NAME} PUBLIC
${PCL_LIBRARY_DIRS}
${OpenCV_LIBRARY_DIRS}
)
add_definitions(${PCL_DEFINITIONS})
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(${PROJECT_NAME})
endif()
I found a solution. I reinstalled Qt and it worked. I don't know if Qt somehow caches the files that are supposed to be used in a project, but reinstalling it at least made the CMake generate again without errors.
Maybe someone knows more about what Qt does there, I am curious to know.

CMakeLists includeT Qt5::QML does not work

This is my CMakelists.txt file.
cmake_minimum_required(VERSION 3.0.2)
project(osm_map)
find_package(catkin REQUIRED COMPONENTS rviz)
find_package(Qt5 COMPONENTS Widgets REQUIRED)
set(QT_LIBRARIES Qt5::Widgets Qt5::Qml)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(SRC_FILES
src/core.cpp
)
add_library(${PROJECT_NAME} ${SRC_FILES})
target_link_libraries(${PROJECT_NAME} ${QT_LIBRARIES} ${catkin_LIBRARIES})
When I try to compile my project with catkin_make -Wno-dev --only-pkg-with-deps osm_map, it seems to find every module of QT (I also tested others not shown below) but not QML. Error message:
CMake Error at osm_map/CMakeLists.txt:53 (target_link_libraries):
Target "osm_map" links to:
Qt5::Qml
but the target was not found. Possible reasons include:
* There is a typo in the target name.
* A find_package call is missing for an IMPORTED target.
* An ALIAS target is missing.
QT QML is installed on my system and the path /usr/include/x86_64-linux-gnu/qt5/QtQml and includes the necessary headers. And idea why I could do? The find_package call for QT is supposed to find all libaries and headers shipped with QT, maybe that is the part that is not working properly for QML?
The problem is in your
find_package(Qt5 COMPONENTS Widgets REQUIRED)
here you asked CMake that you wanted the Widgets component of Qt5 but then you tell it to link with Qml which you did not ask for.
You should change this to the following:
find_package(Qt5 COMPONENTS Widgets Qml REQUIRED)

Unknown CMake command "qtquick_compiler_add_resources"

I want to use QtQuickCompiler for my project, but as soon as I call qtquick_compiler_add_resources I get the following error message:
Unknown CMake command "qtquick_compiler_add_resources".
I use Qt6 and CMake 3.18.2. I have read a lot in the documentation, however everything here should be correct. That's my CMakeLists.txt:
cmake_minimum_required(VERSION 3.14)
project(ProtoPaste LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC OFF)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_PREFIX_PATH "C:/Qt/6.0.0/msvc2019_64/lib/cmake")
set(APP_ICON_RESOURCE_WINDOWS "${CMAKE_CURRENT_SOURCE_DIR}/src/Resource.rc")
option(CLIP_TESTS OFF)
option(CLIP_EXAMPLES OFF)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Quick QuickControls2 Widgets Qt6QuickCompiler REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick QuickControls2 Widgets REQUIRED)
qtquick_compiler_add_resources(RESOURCES
src/qml.qrc
src/images.qrc
)
set(PROJECT_SOURCES
src/main.cpp
src/availableElement.cpp
src/selectedElement.cpp
src/availableElementsModel.cpp
src/selectedElementsModel.cpp
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(ProtoPaste WIN32
${PROJECT_SOURCES}
${RESOURCES}
${APP_ICON_RESOURCE_WINDOWS}
)
else()
add_executable(ProtoPaste WIN32
${PROJECT_SOURCES}
${RESOURCES}
${APP_ICON_RESOURCE_WINDOWS}
)
endif()
add_subdirectory(dependencies)
target_include_directories(ProtoPaste PRIVATE ${clip_SOURCE_DIR})
target_compile_definitions(ProtoPaste PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(ProtoPaste
PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Quick Qt${QT_VERSION_MAJOR}::QuickControls2 Qt${QT_VERSION_MAJOR}::Widgets clip)
What am I doing wrong?
tl;dr
Use qt_add_qml_module instead
Full answer:
With Qt6 it seems qtquick_compiler_add_resources has been removed. The new CMake function within Qt6QmlMacros.cmake is qt_add_qml_module. The function you are looking for had previously been available in Qt5QuickCompilerConfig.cmake. The only mention of that function I could find in Qt6 is in the source file:
declarative/tools/qmlcachegen/Qt6QuickCompilerConfig.cmake.in
The function declared there simply contains this warning:
function(qtquick_compiler_add_resources)
message(WARNING "Use qt_add_qml_module instead of qtquick_compiler_add_resources."
"QML and JavaScript files are automatically compiled when creating a QML"
" module.")
endfunction()
Asking Qt support about this I was directed to this blog:
https://www.qt.io/blog/introduction-to-the-qml-cmake-api
which is very helpful and suggests the reasons for this change.
This new function defaults to compiling the qml into the specified location in the resource file system. It is up to the user to disable compiling.
For those of us supporting CMake-based projects that must work with older Qt versions (e.g. Qt5.6) as well as Qt6.x this means splitting apart qrc files that had previously contained both pure resources (e.g. svg/png) and qml files. The resources can be compiled with qt_add_resources and the qml files can be added with qt_add_qml_module
EDIT: looking a bit more closely, it appears resources could also be compiled in to the qml module via qt_add_qml_module.

Qt Creator Error: cannot find -lopencv_imgcodecs

I have installed opencv, qt, qt creator, cmake on ubuntu 15.10 through VMware on windows.
The opencv is installed in this directory: /home/majidalaeinia/opencv/
The project repository is cloned in this directory: /home/majidalaeinia/Desktop/imgwarp-opencv/
I want to run the project through its CMakeLists.txt in qt creator and when I press Build now on qt creator, I get the following errors:
error: cannot find -lopencv_imgcodecs
error: collect2: error: ld returned 1 exit status
Where is the problem and how can I solve it?
# Majid Alaeinia, from the CMakeLists.txt file you posted it is not specified how CMAKE should find the libraries requested from your project. Also there are no target_link_libraries declared so CMAKE does not know where to link them. Hopefully the following small example template should be helpful for your project:
cmake_minimum_required (VERSION 3.1)
project(yourProject)
find_package( OpenCV REQUIRED )
find_package( Qt5 REQUIRED COMPONENTS Sql )
### this is for c++11
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
### QT stuff if you want a GUI
set(CMAKE_AUTOMOC ON) # autogenerate qt gui features
set(CMAKE_AUTORCC ON) # used for QT resource Files (if you need)
## Additional operation...
# From here you are specifically linking all OpenCV libraries and executables
### Add executables
add_executable(yourExecutable main/main.cpp ui/res/res.qrc ${SRCS} ${UI_HDRS} ${UI_SRCS})
target_link_libraries (yourProject example Qt5::Widgets ${OpenCV_LIBS} Qt5::Sql)
### Add Library
add_library(yourProject_lib SHARED ${SRCS} ${UI_HDRS})
target_link_libraries (yourProject_lib example Qt5::Widgets ${OpenCV_LIBS})
# Majid Alaeinia,I uploaded the repository and went through the code. if you go inside the demo folder and you change the present CMakeLists.txt file with the one I provided below it should compile (It does compile on mine with the provided changes):
project(demo)
cmake_minimum_required(VERSION 2.6)
find_package(Qt5 REQUIRED COMPONENTS Widgets Core)
FIND_PACKAGE( OpenCV REQUIRED )
include_directories(${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR}/lib ${CMAKE_CURRENT_SOURCE_DIR})
set(demo_SRCS main.cpp projfile.cpp deformwin.cpp myimage.cpp singlephotoview.cpp pointspaint.cpp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
#qt5_automoc(${demo_SRCS})
QT5_WRAP_CPP(QOBJ_CPP ${demo_SRCS})
qt5_wrap_ui(helloworld_FORMS_HEADERS deformwin.ui)
add_executable(demo ${demo_SRCS} ${helloworld_FORMS_HEADERS})
target_link_libraries(demo ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} imgwarp-lib opencv_core opencv_imgproc opencv_imgcodecs)
The code in the repository is an old code and still carries Qt4 as main wrappers. I think you probably have Qt5 installed on your computer and in fact the code I provided it will work for Qt5. Use it as a guideline for the other CMakeLists.txt file present inside src folder and change accordingly.
CMake will compile but because it was used Qt4 you need to figure out the most important modules to add, for example the new standard for including QtGui/QApplication is usually substituted by QtWidgets/QApplication
I also wanted to leave my previous answer in case you need a starting point or a initial template. I hope this clarifies a bit more and can get you move forward for your project.

Using Qt inside Clion

I'm trying to use Clion IDE to compile a simple program using Qt library, but I can't figure out how to configure CMakeLists.txt file. (I'm not familiar with cmake and toolchain)
this is my current CMakeLists.txt file:
cmake_minimum_required(VERSION 3.2)
project(MyTest)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(MyTest ${SOURCE_FILES})
# Define sources and executable
set(EXECUTABLE_NAME "MySFML")
add_executable(${EXECUTABLE_NAME} main.cpp)
# Detect and add SFML
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
find_package(SFML 2 REQUIRED system window graphics network audio)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
endif()
It's configured to use SFML library with a "FindSFML.cmake" file and it works fine. (I have copied these files from some tutorial) now I want some help regarding proper CMakeLists.txt configuration to compile programs that are using Qt library (it's more helpful if the files and explanations are provided).
P.S: my current OS is manjaro 0.8.13 and all I could find was explaining configurations in windows environment so I was unable to implement those tutorials.
In addition to #tomvodi's answer, you can use a simpler syntax :
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui).
Then, you don't call qt5_use_modules but instead use the standard command to link :
target_link_libraries(MyTest Qt5::Core Qt5::Widgets Qt5::Gui)
Your CMake project file is missing the Qt packages. You have to add:
find_package( Qt5Core REQUIRED )
find_package( Qt5Widgets REQUIRED )
find_package( Qt5Gui REQUIRED )
and then
qt5_use_modules( MyTest Core Widgets Gui )