CMake: How to compile with different library versions of Qt? - c++

How do you get CMake to compile conditionally with Qt4.8 or Qt5?
In other words, if Qt5 is available then compile with Qt5.
Otherwise if Qt4.8 is available use that.
In my CMake, I have:
find_package(Qt5 COMPONENTS Core Gui Widgets...)
This works fine with my Qt5 builds, but how do I get the same software to build with Qt4.8?
I need something that contains the major version number, eg.:
find_package(Qt $QT_VERSION_MAJOR...)
or to use a condition, such as:
result = find_package(Qt 5...)
if (!result) then find_package(Qt4 ...)
or somehow detect the currently install Qt version.
The error I get for the machine with Qt4.8 installed is (unsurprisingly):
CMake Error at CMakeLists.txt:54 (find_package):
By not providing "FindQt5.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "Qt5", but
CMake did not find one.
Could not find a package configuration file provided by "Qt5" with any of
the following names:
Qt5Config.cmake
What is the best approach here?

Automatically selecting an available version of Qt is fairly easy with the NAME option of the find_package command. The problem is that Qt4 and Qt5 have different names for the same modules.
# We first try to find the main module of Qt4, Qt5 or Qt6
find_package(QT NAMES Qt4 Qt5 Qt6 REQUIRED)
set(QT Qt${QT_VERSION_MAJOR})
# We prepare lists of modules and libraries for different
# versions of Qt
if (QT_VERSION_MAJOR EQUAL 4)
set(APP_QT_MODULES QtCore QtNetwork QtGui QtXml)
set(APP_QT_TARGETS Qt4::QtCore Qt4::QtNetwork Qt4::QtGui Qt4::QtXml)
else ()
set(APP_QT_MODULES Core Network PrintSupport Widgets Xml)
set(APP_QT_TARGETS ${QT}::Core ${QT}::Network ${QT}::PrintSupport ${QT}::Widgets ${QT}::Xml)
endif ()
# Here everything is simple - find the modules we need.
find_package(${QT} REQUIRED ${APP_QT_MODULES})
. . .
. . .
# And at last don't forget to add libraries.
add_executable(my_app app.cpp main.cpp window.cpp)
target_link_libraries(my_app ${APP_QT_TARGETS})
Another problem is that Qt functions have different names too, for example qt4_add_resources and qt5_add_resources. And this is a good reason to wonder whether or not you really need Qt4 support in your project.
Update
We can make Qt function aliases (as is done in Qt since version 5.15).
if (QT_VERSION VERSION_LESS 5.15)
macro(qt_wrap_cpp)
${QT}_wrap_cpp(${ARGV})
endmacro()
macro(qt_add_resources)
${QT}_add_resources(${ARGV})
endmacro()
macro(qt_generate_moc)
${QT}_generate_moc(${ARGV})
endmacro()
endif ()

Related

CLion Not Finding GLUT with CMake

I have a problem that I can't seem to find the settings to modify.
When attempting to find the GLUT package using CLion's CMake utilities on Ubuntu, it does not find GLUT. Using command-line CMake and Makefile commands, however, finds the dependencies perfectly and allows the following to generate and compile:
# CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(mre)
set(CMAKE_CXX_STANDARD 20)
find_package(OpenGL REQUIRED) # Works in CLion and terminal
find_package(GLUT REQUIRED) # Works only in terminal
include_directories(GL)
add_executable(mre mre.cpp)
target_link_libraries(mre -lglut -lGLU -lGL)
// mre.cpp
#include <GL/gl.h>
#include <GL/glut.h>
int main()
{
return 0;
}
Whereas attempting to use these files in a CLion project would cause errors (first unable to find GLUT, mitigated by manually setting library and include variables; then GL/glut.h: No such file or directory, which I am unable to fix).
Does anyone have any suggestions? I'm assuming it's something to do with a working directory or prefixes, but CMAKE_PREFIX_PATH is unset in CLion, and setting it to various values does nothing to solve the problem.
Thanks!
You know something has gone wrong when you write include_directories or -l flags by hand. You should absolutely always link to libraries via their imported targets.
See the documentation:
OpenGL package: https://cmake.org/cmake/help/latest/module/FindOpenGL.html
GLUT package: https://cmake.org/cmake/help/latest/module/FindGLUT.html
Try this revision:
cmake_minimum_required(VERSION 3.16)
project(mre)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
add_executable(mre mre.cpp)
target_link_libraries(mre PRIVATE OpenGL::GL OpenGL::GLU GLUT::GLUT)
target_compile_features(mre PRIVATE cxx_std_20)
As for not being able to find GLUT... just set CMAKE_PREFIX_PATH in CLion's settings to whichever directory on your system contains include/GL/glut.h.
Alternative solution
CLion was installed through the Software Center via Flatpak, which uses some kind of filesystem sandboxing that may be interfering with paths. I tried explicitly allowing /usr and related paths, but had no effect.
I have reinstalled via JetBrains's official archive, which correctly detects GLUT and OpenGL. Their official snap also works properly.

OpenCV linking problems with ROS

I'm trying to compile this project (following the instructions given). When building it with rosmake, I get a bunch of undefined reference to cv::String::deallocate() and undefined reference to cv::String::allocate(unsigned long). I find curious that I'm getting an error just in those functions while the rest of the OpenCV functions seem to be working properly.
I know this happens because the linker can't find the objects where these functions were compiled to, but I'm kind of new to the ROS build system and can't find what's wrong.
I've tried using the CMakeLists.txt file provided, and also adding find_package(OpenCV REQUIRED) and target_link_libraries(xxx xxx ${OpenCV_LIBRARIES}), without that making any difference. I know OpenCV is installed and compiled properly (I've used it before), and I had installed ROS without any problems.
I'm using OpenCV 3, ROS Indigo, Ubuntu 14.04
I had this exact same problem - same error messages, same setup. I've managed to solve it, though I'm not exactly sure of the steps I did which actually contributed. As far as I can tell, it was due to a conflict of OpenCV versions - I think I had old versions of OpenCV cluttering my /usr/include and /usr/local. I uninstalled all OpenCV packages (including the ROS ones) and including my from-source install of OpenCV3 (also in /usr/local). Then I installed the ROS package vision_opencv, which seemed to install OpenCV 2.4.8 (incidentally the one recommended by LSD SLAM). Of course, this could be annoying if you need OpenCV3 for other things, but I now have that as a local install in my home directory (I couldn't figure out how to get ROS to link to this).
I think this solved the problem, the only issue remaining was that I got error messages saying was not found. This was because the package install leaves it in (similar for all module include files), whereas the make install step of the from-source install copies them into the parent folder. To get around this I simply edited the #include in the only file in which it is used (lsd_slam_core/src/IOWrapper/OpenCV/ImageDisplay_OpenCV.cpp). That seemed to solve it!
I hope this helps, I can give further details if needed.
I am using OpenCV 3.1.0(Bleeding edge), ROS Indigo, Ubuntu 14.04.
I ran in to similar trouble when trying to compile LSD-SLAM
I added:
find_package(OpenCV 3.1.0 REQUIRED COMPONENTS core highgui imgproc imgcodecs)
I also added opencv libs to target link libs:
target_link_libraries(lsdslam ${FABMAP_LIB} g2o_core g2o_stuff csparse cxsparse g2o_solver_csparse g2o_csparse_extension g2o_types_sim3 g2o_types_sba X11 opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs)
Here is my CMakeLists.txt file:
cmake_minimum_required(VERSION 2.8.12)
project(lsd_slam_core)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
find_package(OpenCV 3.1.0 REQUIRED COMPONENTS core highgui imgproc imgcodecs)
# Set the build type. Options are:
# Coverage : w/ debug symbols, w/o optimization, w/ code-coverage
# Debug : w/ debug symbols, w/o optimization
# Release : w/o debug symbols, w/ optimization
# RelWithDebInfo : w/ debug symbols, w/ optimization
# MinSizeRel : w/o debug symbols, w/ optimization, stripped binaries
set(ROS_BUILD_TYPE Release)
rosbuild_init()
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
find_package(Eigen3 REQUIRED)
find_package(SuiteParse REQUIRED) # Apparently needed by g2o
find_package(X11 REQUIRED)
# FabMap
# uncomment this part to enable fabmap
#add_subdirectory(${PROJECT_SOURCE_DIR}/thirdparty/openFabMap)
#include_directories(${PROJECT_SOURCE_DIR}/thirdparty/openFabMap/include)
#add_definitions("-DHAVE_FABMAP")
#set(FABMAP_LIB openFABMAP )
# Dynamic Reconfigure Services
rosbuild_find_ros_package(dynamic_reconfigure)
include(${dynamic_reconfigure_PACKAGE_PATH}/cmake/cfgbuild.cmake)
gencfg()
# SSE flags
rosbuild_check_for_sse()
add_definitions("-DUSE_ROS")
add_definitions("-DENABLE_SSE")
# Also add some useful compiler flag
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} ${SSE_FLAGS} -march=native -std=c++0x"
)
# Set source files
set(lsd_SOURCE_FILES
${PROJECT_SOURCE_DIR}/src/DataStructures/Frame.cpp
${PROJECT_SOURCE_DIR}/src/DataStructures/FramePoseStruct.cpp
${PROJECT_SOURCE_DIR}/src/DataStructures/FrameMemory.cpp
${PROJECT_SOURCE_DIR}/src/SlamSystem.cpp
${PROJECT_SOURCE_DIR}/src/LiveSLAMWrapper.cpp
${PROJECT_SOURCE_DIR}/src/DepthEstimation/DepthMap.cpp
${PROJECT_SOURCE_DIR}/src/DepthEstimation/DepthMapPixelHypothesis.cpp
${PROJECT_SOURCE_DIR}/src/util/globalFuncs.cpp
${PROJECT_SOURCE_DIR}/src/util/SophusUtil.cpp
${PROJECT_SOURCE_DIR}/src/util/settings.cpp
${PROJECT_SOURCE_DIR}/src/util/Undistorter.cpp
${PROJECT_SOURCE_DIR}/src/Tracking/Sim3Tracker.cpp
${PROJECT_SOURCE_DIR}/src/Tracking/Relocalizer.cpp
${PROJECT_SOURCE_DIR}/src/Tracking/SE3Tracker.cpp
${PROJECT_SOURCE_DIR}/src/Tracking/TrackingReference.cpp
${PROJECT_SOURCE_DIR}/src/IOWrapper/Timestamp.cpp
${PROJECT_SOURCE_DIR}/src/GlobalMapping/FabMap.cpp
${PROJECT_SOURCE_DIR}/src/GlobalMapping/KeyFrameGraph.cpp
${PROJECT_SOURCE_DIR}/src/GlobalMapping/g2oTypeSim3Sophus.cpp
${PROJECT_SOURCE_DIR}/src/GlobalMapping/TrackableKeyFrameSearch.cpp
)
set(SOURCE_FILES
${lsd_SOURCE_FILES}
${PROJECT_SOURCE_DIR}/src/IOWrapper/ROS/ROSImageStreamThread.cpp
${PROJECT_SOURCE_DIR}/src/IOWrapper/ROS/ROSOutput3DWrapper.cpp
${PROJECT_SOURCE_DIR}/src/IOWrapper/OpenCV/ImageDisplay_OpenCV.cpp
)
include_directories(
${EIGEN3_INCLUDE_DIR}
${PROJECT_SOURCE_DIR}/src
${PROJECT_SOURCE_DIR}/thirdparty/Sophus
${CSPARSE_INCLUDE_DIR} #Has been set by SuiteParse
${CHOLMOD_INCLUDE_DIR} #Has been set by SuiteParse
)
# build shared library.
rosbuild_add_library(lsdslam SHARED ${SOURCE_FILES})
target_link_libraries(lsdslam ${FABMAP_LIB} g2o_core g2o_stuff csparse cxsparse g2o_solver_csparse g2o_csparse_extension g2o_types_sim3 g2o_types_sba X11 opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs)
rosbuild_link_boost(lsdslam thread)
# build live ros node
rosbuild_add_executable(live_slam src/main_live_odometry.cpp)
target_link_libraries(live_slam lsdslam)
# build image node
rosbuild_add_executable(dataset_slam src/main_on_images.cpp)
target_link_libraries(dataset_slam lsdslam)
Do you have multi-versions Opencv? if you have, maybe you should add the path to OpenCVConfig.cmake into CmakeList.txt. Just under the cmake_minimum_required(VERSION 2.8.12),like so:
set(OpenCV_DIR "/home/ubuntu/src/opencv-3.1.0/build")
That's all.
I also had the samme issue. Could not comment Osian's solution due to lack of rep, but this is the procedure I used:
sudo apt-get remove libopencv*
If you for some reason have OpenCV installed from source, enter your build directory and do:
sudo make uninstall
Then finally
sudo apt-get install ros-indigo-desktop-full

Qt Creator cannot find library with custom cmake step

In Qt Creator (version 3.0.1, with Qt version 5.2.1), I have made a custom cmake step (instead of qmake), with the following CMakeLists.txt file:
cmake_minimum_required (VERSION 2.8)
add_executable (myapp source.cpp)
target_link_libraries(myapp dl)
In my source.cpp file, I have the following code:
#include <dlfcn.h>
int main()
{
dlopen("mylibrary.so", RTLD_NOW|RTLD_GLOBAL);
return 0;
}
And mylibrary.so is located in /usr/lib.
When I compile this using cmake and make from the command line, it compiles as expected. However, if I try to build this in Qt Creator, I receive the following error:
undefined reference to `dlopen'
This suggests that Qt Creator does not know where to look to find libdl.so, which is in /usr/lib/x86_64-linux-gnu.
So my question is: Why does running cmake and make from the command line work, whereas building in Qt Creator does not work? And how do I tell Qt Creator where to search for libdl.so?
First of all, you should use QLibrary in Qt software for dealing with dynamic loading, lookup and the like. You would also spare the hassle that you are seeing now.
Secondly, you can use this, but it is a bit hard-wiring things, admittedly:
target_link_libraries(myapp /usr/lib/x86_64-linux-gnu/libdl.so)
Thirdly, the even better approach would be to use some find module for this as follows:
# - Find libdl
# Find the native LIBDL includes and library
#
# LIBDL_INCLUDE_DIR - where to find dlfcn.h, etc.
# LIBDL_LIBRARIES - List of libraries when using libdl.
# LIBDL_FOUND - True if libdl found.
IF (LIBDL_INCLUDE_DIR)
# Already in cache, be silent
SET(LIBDL_FIND_QUIETLY TRUE)
ENDIF (LIBDL_INCLUDE_DIR)
FIND_PATH(LIBDL_INCLUDE_DIR dlfcn.h)
SET(LIBDL_NAMES dl libdl ltdl libltdl)
FIND_LIBRARY(LIBDL_LIBRARY NAMES ${LIBDL_NAMES} )
# handle the QUIETLY and REQUIRED arguments and set LIBDL_FOUND to TRUE if
# all listed variables are TRUE
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibDL DEFAULT_MSG LIBDL_LIBRARY LIBDL_INCLUDE_DIR)
IF(LIBDL_FOUND)
SET( LIBDL_LIBRARIES ${LIBDL_LIBRARY} )
ELSE(LIBDL_FOUND)
SET( LIBDL_LIBRARIES )
ENDIF(LIBDL_FOUND)
MARK_AS_ADVANCED( LIBDL_LIBRARY LIBDL_INCLUDE_DIR )
and then you can find it as follows given that you have it in your cmake module path:
find_package(LIBDL REQUIRED)

CMake miss QT_DEFINITIONS variable for Qt5

CMake have special variable ${QT_DEFINITIONS} for Qt4 which contains qt defines such as QT_NO_DEBUG and QT_DEBUG. There is no such option for Qt5.
How can I add standard Qt5 defines in cmake?
Please check <component>_DEFINITIONS / <component>_COMPILE_DEFINITIONS.
There's an example using Qt Widgets and CMake here. The two essential parts:
# Find the QtWidgets library
find_package(Qt5Widgets)
# Add the include directories for the Qt 5 Widgets module to
# the compile lines.
include_directories(${Qt5Widgets_INCLUDE_DIRS})
Calling find_package() will set those variables for you:
Qt5Widgets_VERSION_STRING
Qt5Widgets_LIBRARIES List of libraries for use with the target_link_libraries command, for example.
Qt5Widgets_INCLUDE_DIRS List of libraries for use with the include_directories command, for example.
Qt5Widgets_DEFINITIONS List of definitions for use with add_definitions, for example.
Qt5Widgets_COMPILE_DEFINITIONS List of definitions for use with the COMPILE_DEFINITIONS target property.
Qt5Widgets_FOUND Boolean describing whether the module was found successfully.
Qt5Widgets_EXECUTABLE_COMPILE_FLAGS String of flags to be used when building executables.
(Quoted from link above; the name depends on what you search in your find_package() call)

Qt w/ Cmake: set(QT_USE_QTWEBKIT TRUE) not working

I'm attempting to build a Qt-based application using cmake (It's what Kdevelop gave me). I tried to use a QWebView;
QWebView *webView = new QWebView( this );
webView->load(QUrl("http://google.ca"));
But it failed with Undefined Reference errors...
undefined reference to `QWebView::QWebView(QWidget*)'
undefined reference to `QWebView::load(QUrl const&)'
I looked it up and I needed to add QTWEBKIT to my project, but all the solutions said to add it to my .pro file... And I'm not using .pro. In the QT documentation it said to add "set(QT_USE_QTWEBKIT TRUE)" to my CMAKE file, this is my CMakeLists.txt file now:
#-------------------------------------------------------------------------------
# Corrections Tool CMAKE list
#-------------------------------------------------------------------------------
project(corrections)
# Versioning Requirements
#-------------------------------------------------------------------------------
cmake_minimum_required(VERSION 2.6)
find_package(Qt4 REQUIRED)
# Include QT Librtaries
#-------------------------------------------------------------------------------
set(QT_USE_QTWEBKIT TRUE)
# Set Sources
#-------------------------------------------------------------------------------
set(corrections_SRCS corrections.cpp main.cpp utilities.cpp prettySplash.cpp)
#The Rest
#-------------------------------------------------------------------------------
include_directories(${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})
qt4_automoc(${corrections_SRCS})
add_executable(corrections ${corrections_SRCS})
target_link_libraries(corrections ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY})
install(TARGETS corrections RUNTIME DESTINATION bin)
But I'm still getting the errors, so either I did it wrong, in the wrong place, etc. I've also cleaned out and reconfigured my project several times making sure I wasn't using a bad generated makefile.
How would I either fix my cmake config to actually work, or convert my project to using .pro (with minimum stress & heartache)?
Thank you.
The canonical way to select Qt components in CMake is to specify the them in the find_package call and then to include ${QT_USE_FILE}
FIND_PACKAGE( Qt4 COMPONENTS QtWebKit REQUIRED )
INCLUDE( ${QT_USE_FILE} )
...
TARGET_LINK_LIBRARIES( corrections ${QT_LIBRARIES} )
This already configures the include directories and sets ${QT_LIBRARIES} to contain all relevant Qt libraries (i.e. your selected component and all Qt libraries it depends on).
So you don't need to manually add the libraries by listing them individually as you did in your example.
Edit:
Additional explaination:
The COMPONENTparameter to FIND_PACKAGE actually does the same as your manual call to set QT_USE_WEBKIT. But this variable is only evaluated/used in UseQt4.cmake which is included (and "executed") by the INCLUDEcommand. See CMake documentation of FindQt4 for details.
You do not appear to be linking against all of the Qt libraries. Use ${QT_LIBRARIES} instead of ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} in your target_link_libraries
target_link_libraries(corrections ${QT_LIBRARIES} )