当前位置:   article > 正文

GNU开发工具——CMake构建Qt工程实践

qt如何gnu

GNU开发工具——CMake构建Qt工程实践

一、CMake构建Qt工程

1、Qt工程源码

创建Migration目录,在目录下创建main.cpp文件:

  1. #include <QApplication>
  2. #include <QLabel>
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication app(argc, argv);
  6. QLabel label(QString("Hello Qt%1!").arg(int(QT_VERSION >> 16)));
  7. label.setAlignment(Qt::AlignCenter);
  8. label.resize(200, 100);
  9. label.show();
  10. return app.exec();
  11. }

2、Qt4和Qt5兼容宏

编写兼容Qt4和Qt5的CMake宏,QtMigration.cmake文件如下:

  1. # 定义宏QT_USE_MODULES
  2. macro(QT_USE_MODULES _target)
  3. # Enable AUTOMOC
  4. set_target_properties(${_target} PROPERTIES AUTOMOC TRUE)
  5. # Local variables
  6. set(_modules_qt4)
  7. set(_modules_qt5)
  8. # Prepare modules
  9. foreach(_module ${ARGN})
  10. list(APPEND _modules_qt4 Qt${_module})
  11. list(APPEND _modules_qt5 ${_module})
  12. if(_module MATCHES "Gui")
  13. list(APPEND _modules_qt5 "Widgets")
  14. endif(_module MATCHES "Gui")
  15. endforeach(_module ${ARGN})
  16. list(REMOVE_DUPLICATES _modules_qt4)
  17. list(REMOVE_DUPLICATES _modules_qt5)
  18. # Find Qt libraries
  19. find_package(Qt5 QUIET COMPONENTS ${_modules_qt5})
  20. if(Qt5_FOUND)
  21. qt5_use_modules(${_target} ${_modules_qt5})
  22. else(Qt5_FOUND)
  23. find_package(Qt4 QUIET COMPONENTS ${_modules_qt4})
  24. if(Qt4_FOUND OR QT4_FOUND)
  25. include(${QT_USE_FILE})
  26. include_directories(${QT_INCLUDES})
  27. add_definitions(${QT_DEFINITIONS})
  28. target_link_libraries(${_target} ${QT_LIBRARIES})
  29. endif(Qt4_FOUND OR QT4_FOUND)
  30. endif(Qt5_FOUND)
  31. endmacro(QT_USE_MODULES)

3、CMakeLists.txt编写

编写工程CMakeLists.txt文件,内容如下:

  1. cmake_minimum_required(VERSION 2.8.9)
  2. project(Migration)
  3. list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR})
  4. aux_source_directory(. SRC_LIST)
  5. add_executable(${PROJECT_NAME} ${SRC_LIST})
  6. include(QtMigration.cmake)
  7. QT_USE_MODULES(${PROJECT_NAME} Core Gui)

4、构建工程

Migration工程内创建build目录,进入build目录,进行构建。

  1. cmake ..
  2. make

5、CMake的Qt相关变量

对于Qt4,使用FIND_PACKAGE后,会生成有效的Qt4_FOUND,QT_USE_FILE,QT_INCLUDES,QT_LIBRARIES变量。

  1. FIND_PACKAGE(Qt4 REQUIRED Core Gui)
  2. if(Qt4_FOUND)
  3. INCLUDE(${QT_USE_FILE})
  4. INCLUDE_DIRECTORIES(${QT_INCLUDES})
  5. TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${QT_LIBRARIES})
  6. endif()

对于Qt5,使用FIND_PACKAGE后,会生成有效的Qt5_FOUND,Qt5Core_INCLUDE_DIRS,Qt5Xml_INCLUDE_DIRS,Qt5Gui_INCLUDE_DIRS,Qt5Widgets_INCLUDE_DIRS,Qt5OpenGL_INCLUDE_DIRS,Qt5Widgets_LIBRARIES,Qt5Core_LIBRARIES,Qt5Gui_LIBRARIES,Qt5Xml_LIBRARIES,Qt5OpenGL_LIBRARIES等相应模块的变量。

  1. FIND_PACKAGE(Qt5 REQUIRED Core Gui Widgets OpenGL Xml)
  2. if(Qt5_FOUND)
  3. INCLUDE_DIRECTORIES(${Qt5Core_INCLUDE_DIRS} ${Qt5Xml_INCLUDE_DIRS}
  4. ${Qt5Gui_INCLUDE_DIRS} ${Qt5Widgets_INCLUDE_DIRS} ${Qt5OpenGL_INCLUDE_DIRS})
  5. #定义QT_LIBRARIES
  6. SET(QT_LIBRARIES ${Qt5Widgets
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/658754
推荐阅读
  

闽ICP备14008679号