
find_package (Qt4 COMPONENTS QTCORE QTGUI)find_package (Qt5Widgets)find_package (Qt5Declarative)find_package(Qt5 COMPONENTS Widgets Declarative)find_package , Qt 4 users were given the opportunity to use CMake variables: ${QT_INCLUDES} to set additional directories when compiling and ${QT_LIBRARIES} or ${QT_GUI_LIBRARIES} when linking.${QT_USE_FILE} , for the "semi-automatic" inclusion of the necessary directories and the required define.${Qt5Widgets_INCLUDE_DIRS}, ${Qt5Widgets_LIBRARIES}, ${Qt5Declarative_INCLUDE_DIRS}, ${Qt5Declarative_LIBRARIES} and so on for each module used.-reduce-relocations option is now enabled by default. For this reason, compilation was performed with the -Bsymbolic-functions option, which makes the pointer comparison function ineffective if the -fPIE flag was not added when building executable modules, or -fPIC , when building libraries for position-independent code.-no-reduce-relocations option and avoid this problem, but new problems will arise when you add flags to the compiler for position-independent code that you can avoid using CMake:set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")-fPIE or an empty string, depending on how Qt was configured. However, the -fPIE flag is for executable programs only and should not be used for libraries.-fPIC , even when building an executable module will not cause failures, but this option should not be first.set(CMAKE_CXX_FLAGS "-fPIC") cmake_minimum_required(2.8.7) project(hello-world) # Tell CMake to run moc when necessary: set(CMAKE_AUTOMOC ON) # As moc files are generated in the binary dir, tell CMake # to always look for includes there: set(CMAKE_INCLUDE_CURRENT_DIR ON) # Widgets finds its own dependencies (QtGui and QtCore). find_package(Qt5Widgets REQUIRED) # The Qt5Widgets_INCLUDES also includes the include directories for # dependencies QtCore and QtGui include_directories(${Qt5Widgets_INCLUDES}) # We need add -DQT_WIDGETS_LIB when using QtWidgets in Qt 5. add_definitions(${Qt5Widgets_DEFINITIONS}) # Executables fail to build with Qt 5 in the default configuration # without -fPIE. We add that here. set(CMAKE_CXX_FLAGS "${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}") add_executable(hello_world main.cpp mainwindow.cpp) # The Qt5Widgets_LIBRARIES variable also includes QtGui and QtCore target_link_libraries(hello_world ${Qt5Widgets_LIBRARIES}) cmake_minimum_required(2.8.8) project(hello-world) # Tell CMake to run moc when necessary: set(CMAKE_AUTOMOC ON) # As moc files are generated in the binary dir, tell CMake # to always look for includes there: set(CMAKE_INCLUDE_CURRENT_DIR ON) # Widgets finds its own dependencies. find_package(Qt5Widgets REQUIRED) add_executable(hello_world main.cpp mainwindow.cpp) qt5_use_modules(hello_world Widgets) qt5_use_modules function encapsulates the entire installation necessary to use Qt. It can be used with several arguments at once for brevity, for example:qt5_use_modules(hello_world Widgets Declarative)TARGET = hello_worldQT += widgets declarative add_executable(hello_world main.cpp mainwindow.cpp) add_library(hello_library lib.cpp) add_executable(hello_coretest test.cpp) find_package(Qt5Widgets) qt5_use_package(hello_world Widgets) qt5_use_package(hello_library Core) qt5_use_package(hello_coretest Test) -fPIE not used when building the hello_library library and -DQT_GUI_LIB not used when building the hello_coretest.FindQt4.cmake file. This file takes responsibility for finding Qt in the system so that you can simply call:find_package(Qt4)${QT_INCLUDES} and ${QT_QTGUI_LIBRARIES} variables available. One of the disadvantages of this file is that it could become outdated. For example, when Qt 4.6 was released in December 2009, it included a new QtMultimedia module. There was no support for this module until CMake 2.8.2, released in June 2010.Source: https://habr.com/ru/post/181838/
All Articles