日志
qt530 CMake Manual
2014-05-22 10:31
CMake Manual CMake is a tool that helps simplify the build process for development projects across different platforms. CMake automates the generation of buildsystems such as Makefiles and Visual Studio project files.CMake is a 3rd party tool with its own documentation. The rest of this manual details the specifics of how to use Qt 5 with CMake. The minimum version required to use Qt5 is CMake 2.8.3, but 2.8.11 is recommended. Getting Started The first requirement when using CMake is to use find_package to locate the libraries and header files shipped with Qt. These libraries and header files can then be used to build libraries and applications based on Qt.The recommended way to use Qt libraries and headers with CMake 2.8.11 is to use the target_link_libraries command. In CMake 2.8.11 and later versions, this command automatically adds appropriate include directories, compile definitions, the position-independent-code flag, and links to the qtmain.lib library on Windows.To build a helloworld GUI executable, typical usage would be:cmake_minimum_required(VERSION 2.8.11)project(testproject)# Find includes in corresponding build directoriesset(CMAKE_INCLUDE_CURRENT_DIR ON)# Instruct CMake to run moc automatically when needed.set(CMAKE_AUTOMOC ON)# Find the QtWidgets libraryfind_package(Qt5Widgets)# Tell CMake to create the helloworld executableadd_executable(helloworld WIN32 main.cpp)# Use the Widgets module from Qt 5.target_link_libraries(helloworld Qt5::Widgets)Note that setting the minimum required CMake version to 2.8.11 is required for automatic linking to the qtmain.lib library on Windows.In order for find_package to be successful, Qt 5 must be found below the CMAKE_PREFIX_PATH, or the Qt5<Module>_DIR must be set in the CMake cache to the location of the Qt5WidgetsConfig.cmake file. The easiest way to use CMake is to set the CMAKE_PREFIX_PATH environment variable to the install prefix of Qt 5.The CMAKE_AUTOMOC setting runs moc automatically when required. For more on this feature see the CMake AUTOMOC documentation Imported targets Imported targets are created for each Qt module. Imported target names should be preferred instead of using a variable like Qt5<Module>_LIBRARIES in CMake commands such as target_link_libraries. The actual path to the library can be obtained using the LOCATION property:find_package(Qt5Core)get_target_property(QtCore_location Qt5::Core LOCATION)Note however that it is rare to require the full location to the library in CMake code. Most CMake APIs are aware of imported targets and can automatically use them instead of the full path.Each module in Qt 5 has a library target with the naming convention Qt5::<Module> which can be used for this purpose.Imported targets are created with the configurations Qt was configured with. That is, if Qt was configured with the -debug switch, an imported target with the configuration DEBUG will be created. If Qt was configured with the -release switch an imported target with the configuration RELEASE will be created. If Qt was configured with the -debug-and-release switch (the default on windows), then imported targets will be created with both RELEASE and DEBUG configurations.If your project has custom CMake build configurations, it may be necessary to set a mapping from your custom configuration to either the debug or release Qt configuration.find_package(Qt5Core)set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_RELEASE} -fprofile-arcs -ftest-coverage")# set up a mapping so that the Release configuration for the Qt imported target is# used in the COVERAGE CMake configuration.set_target_properties(Qt5::Core PROPERTIES MAP_IMPORTED_CONFIG_COVERAGE "RELEASE")Plugins are also available as IMPORTED targets in CMake. The Qt Network, Qt SQL, Qt GUI, and Qt Widgets modules have plugins associated. They provide a list of plugins in the Qt5<Module>_PLUGINS variable.foreach(plugin ${Qt5Network_PLUGINS})get_target_property(_loc ${plugin} LOCATION)message("Plugin ${plugin} is at location ${_loc}")endforeach() Using Qt 5 with CMake older than 2.8.11 The recommended way to use Qt libraries and headers with CMake prior to 2.8.11 is to use the qt5_use_modules macro.Note that it is necessary to use find_package to find a Qt module before using the macro. See the documentation for the CMake find_package Documentation command for the full options it supports.The qt5_use_modules macro encapsulates all of the variable usage required to use a Qt module. It automatically finds the modules given to it on the command line if they have not already been found.find_package(Qt5Widgets)add_executable(helloworld WIN32 main.cpp)qt5_use_modules(helloworld Widgets) Using Qt 5 with CMake older than 2.8.9 If using CMake older than 2.8.9, the qt5_use_modules macro is not available. Attempting to use it will result in an error.To use Qt 5 with versions of CMake older than 2.8.9, it is necessary to use the target_link_libraries, include_directories, and add_definitions commands, and to manually specify moc requirements with either qt5_generate_moc or qt5_wrap_cpp:cmake_minimum_required(VERSION 2.8.3)project(testproject)# Find includes in corresponding build directoriesset(CMAKE_INCLUDE_CURRENT_DIR ON)# Find the QtWidgets libraryfind_package(Qt5Widgets)# Add the include directories for the Qt 5 Widgets module to# the compile lines.include_directories(${Qt5Widgets_INCLUDE_DIRS})# Use the compile definitions defined in the Qt 5 Widgets moduleadd_definitions(${Qt5Widgets_DEFINITIONS})# Add compiler flags for building executables (-fPIE)set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")qt5_generate_moc(main.cpp main.moc)# Tell CMake to create the helloworld executableadd_executable(helloworld main.cpp main.moc)#Link the helloworld executable to the Qt 5 widgets library.target_link_libraries(helloworld Qt5::Widgets)It is also necessary when using an older CMake to add Qt5<Module>_EXECUTABLE_COMPILE_FLAGS to the CMAKE_CXX_FLAGS so that the -fPIE flags are added to the compile flags if necessary (as is the default with Qt 5).If not using the qt5_use_modules macro, and if using CMake 2.8.9 or later, the POSITION_INDEPENDENT_CODE property can be set on targets using Qt instead, or it can be set globally for all targets. Note that this is not necessary with CMake 2.8.11:find_package(Qt5Core)add_executable(exe1 ${exe1_SRCS})# Set the POSITION_INDEPENDENT_CODE property for the exe1 target...set_target_properties(exe1 PROPERTIES POSITION_INDEPENDENT_CODE ON)# Or set it globally for all targets:set(CMAKE_POSITION_INDEPENDENT_CODE ON)add_executable(exe2 ${exe2_SRCS})add_executable(exe3 ${exe3_SRCS})Note that it may be necessary to enable POSITION_INDEPENDENT_CODE globally in order to use try_compile with Qt code, or any wrapper macros around it such as check_cxx_source_compiles etc. As position independent code is a platform-specific and Qt-configuration-specific concept, the Qt5_POSITION_INDEPENDENT_CODE property can be used to check whether it is required.if (Qt5_POSITION_INDEPENDENT_CODE)set(CMAKE_POSITION_INDEPENDENT_CODE ON)endif() Variable Reference Module variables The result of a find_package call is that imported targets will be created for use with target_link_libraries, some variables will be populated with information required to configure the build, and macros will be made available for use. The name of the imported target for each module matches the name of the module with a prefix of 'Qt5::', for example Qt5::Widgets. All of the package-specific variables have a consistent name with a prefix of the name of the package. For example, find_package(Qt5Widgets) will make the following variables available if successfully found:
Installation variables Additionally, several other variables are available which do not relate to a particular package, but to the Qt installation itself.
Macro Reference Qt5Core macros Macros available when Qt5Core is found.
Qt5Widgets macros Macros available when Qt5Widgets is found.
Qt5DBus macros Macros available when Qt5DBus is found.
Qt5LinguistTools macros Macros available when Qt5LinguistTools is found.
|
上一篇: Qt 5 C++ API changes