CMakeLists includeT Qt5::QML does not work - c++

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)

Related

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.

What does this strange "/usr/bin/ld: cannot find -lXAW_LIBRARY-NOTFOUND" error mean?

I created a ROS2 rviz plugin in C++ which I need to compile into a shared library (.so) using cmake. I already have a working CMakeLists.txt (see below) which creates a static library (.a); I need it to be shared, though.
However, when I add the SHARED keyword to the add_library macro (commented out in the code below), it throws this strange error:
/usr/bin/ld: cannot find -lXAW_LIBRARY-NOTFOUND
Now, I have looked at the many "/usr/bin/ld: cannot find [some library]" questions here on SO (like this), but my error seems to be more strange since it seems to contain an error ("-lXAW_LIBRARY-NOTFOUND") IN THE error (/usr/bin/ld: cannot find...). I mean, why is he even looking for a library called LIBRARY_NOTFOUND??
I'm on Ubuntu xenial 16.04 using cmake 3.10.
CMakeLists.txt:
project(traffic_sign_delegation_manager)
set(CMAKE_CXX_STANDARD 17)
if(NOT WIN32)
add_definitions(-fPIC)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic -Wno-deprecated-declarations)
endif()
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rviz_common REQUIRED)
find_package(std_msgs REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(rosidl_generator_cpp)
find_package(pluginlib REQUIRED)
find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5OpenGL REQUIRED)
find_package(Qt5 REQUIRED COMPONENTS Widgets)
set(msg_files
"msg/TrafficSignList.msg"
"msg/TrafficSign.msg"
"msg/TrafficSignSetList.msg"
"msg/TrafficSignSet.msg"
"msg/TrafficSignSetStatus.msg"
"msg/TrafficSignsManaged.msg"
"msg/AccLever2.msg"
"msg/VehicleOdometry.msg"
)
rosidl_generate_interfaces(${PROJECT_NAME}
${msg_files}
DEPENDENCIES std_msgs
)
link_directories(${ament_cmake_LIBRARY_DIRS})
add_definitions(-DQT_NO_KEYWORDS)
qt5_wrap_ui(QT_UI_FILES ui/traffic_sign_delegation_manager_panel.ui)
qt5_wrap_ui(QT_UI_FILES ui/traffic_sign_list_item.ui)
qt5_add_resources(QT_QRC_FILES ui/traffic_sign_delegation_manager.qrc)
set_property(SOURCE traffic_sign_delegation_manager_panel.h PROPERTY SKIP_AUTOMOC ON)
set_property(SOURCE draw_area.h PROPERTY SKIP_AUTOMOC ON)
set_property(SOURCE adv_interaction_groupbox.h PROPERTY SKIP_AUTOMOC ON)
set_property(SOURCE traffic_sign_delegation_manager_display.h PROPERTY SKIP_AUTOMOC ON)
add_library(delegator_lib # SHARED # <=== WHY IS THIS NOT WORKING?
vec2d.cpp
vec2d.h
traffic_sign_delegation_manager_panel.cpp
traffic_sign_delegation_manager_panel.h
draw_area.cpp
draw_area.h
traffic_sign_delegation_manager_display.cpp
traffic_sign_delegation_manager_display.h
adv_interaction_groupbox.cpp
adv_interaction_groupbox.h
ui/traffic_sign_list_item.ui
ui/traffic_sign_delegation_manager_panel.ui
${QT_UI_FILES}
${MOC_FILES}
)
rosidl_target_interfaces(delegator_lib ${PROJECT_NAME} "rosidl_typesupport_cpp")
target_include_directories(delegator_lib PUBLIC
${rvizCommon_DIR}
${rosidl_generator_cpp_INCLUDE_DIRS}
${ament_cmake_INCLUDE_DIRS}
${rviz2_INCLUDE_DIRS}
${rviz_common_INCLUDE_DIRS}
${FREETYPE_INCLUDE_DIRS}
${Qt5_INCLUDE_DIRS}
)
target_link_libraries(delegator_lib
rviz_common::rviz_common
)
target_compile_definitions(delegator_lib PRIVATE "RVIZ_DEFAULT_PLUGINS_BUILDING_LIBRARY")
target_compile_definitions(delegator_lib PUBLIC "PLUGINLIB__DISABLE_BOOST_FUNCTIONS")
pluginlib_export_plugin_description_file(rviz_common plugin_description.xml)
ament_target_dependencies(delegator_lib
geometry_msgs
laser_geometry
nav_msgs
map_msgs
rclcpp
resource_retriever
urdf
visualization_msgs
)
ament_export_include_directories(${INCLUDE_DIRS} ${ament_cmake_INCLUDE_DIRS} include)
ament_export_interfaces(delegator_lib HAS_LIBRARY_TARGET)
ament_export_dependencies(
Qt5
rviz_common
geometry_msgs
laser_geometry
map_msgs
nav_msgs
rclcpp
urdf
visualization_msgs
rosidl_generator_cpp
)
install(FILES plugin_description.xml
DESTINATION share/${PROJECT_NAME})
install(DIRECTORY images
DESTINATION share/${PROJECT_NAME})
install(DIRECTORY ui
DESTINATION share/${PROJECT_NAME}
PATTERN "*.ui"
EXCLUDE)
install(
TARGETS delegator_lib
EXPORT delegator_lib
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
INCLUDES DESTINATION include
)
install(
DIRECTORY "${CMAKE_SOURCE_DIR}/icons"
DESTINATION "share/${PROJECT_NAME}"
)
ament_package()
Please note: This question is not about ROS; I am not a cmake wizard, so probably I'm just doing something terribly wrong in cmake... I already asked a more broad version of this question on answers.ros, but it appears to be too cmake-specific or something. Anyway, I did not get an answer there. (The above code is not an MWE, sorry; I could create one if neccessary, but it would require ROS2 to compile...)
Normally that error pops up if you are giving some library names as arguments to the target_link_libraries function.
Either,
The names are not correct or,
The path is not or,
The libraries are not installed.
I'd look at target_link_libraries(delegator_lib rviz_common::rviz_common) and link_directories(${ament_cmake_LIBRARY_DIRS}) as suspects.
In CMake you can use MESSAGE command to do debugging of sorts where you can display the values of the CMake variables to check if they make sense with what is on the system.
Also you can try installing the XAW library like this sudo apt-get install libxaw7-dev. Could be that one of the libraries that you link to has a dependency on the XAW library.

CMake: linking against a library linked to Qt5

I have a library which uses Qt5. I build it with this CMakeLists.txt file:
cmake_minimum_required (VERSION 3.0)
project (testLib LANGUAGES CXX)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5 REQUIRED Widgets)
add_library(testLib SHARED
src/TestClass.cpp
src/TestClass.h
)
target_include_directories(testLib PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/src
)
target_link_libraries(testLib PUBLIC Qt5::Widgets)
export(TARGETS testLib FILE testLib-exports.cmake)
Now I try to link an executable against this library in the build path. This is what I've tried so far:
cmake_minimum_required (VERSION 3.0)
project(TestProject)
add_executable(myexec src/main.cpp )
include(/path/to/testLib-exports.cmake)
target_link_libraries(myexec testLib)
I am getting this error:
Target "myexec" links to target "Qt5::Widgets" but the target was not
found. Perhaps a find_package() call is missing for an IMPORTED
target, or an ALIAS target is missing?
I don't want to explicitly use find_package() in myexec's cmake file, but want it to be transitive. How should I link against testLib then? If the answer is not in the build path it is fine.
EDIT: I changed the export line to:
export(TARGETS testLib FILE testLib-exports.cmake EXPORT_LINK_INTERFACE_LIBRARIES)
This seems to be exactly what I need, but the generated files with and without EXPORT_LINK_INTERFACE_LIBRARIES are identical. Both if them say
This file does not depend on other imported targets which have
been exported from the same project but in a separate export set.
As far as I understand, it should use testLib's target LINK_INTERFACE_LIBRARIES property, but I noticed that it is NOTFOUND. maybe this is the problem? However, INTERFACE_LINK_LIBRARIES has Qt5::Widgets.

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 )