当前位置:   article > 正文

Qt cmake_qt导入cmake工程

qt导入cmake工程

搜索源文件


  1. #搜索源文件
  2. file(GLOB_RECURSE PROJECT_SOURCES
  3. ${PROJECT_SOURCE_DIR}/*.c
  4. ${PROJECT_SOURCE_DIR}/*.cpp
  5. ${PROJECT_SOURCE_DIR}/*.cc
  6. ${PROJECT_SOURCE_DIR}/*.h
  7. ${PROJECT_SOURCE_DIR}/*.hpp
  8. ${PROJECT_SOURCE_DIR}/*.ui
  9. ${PROJECT_SOURCE_DIR}/*.qrc
  10. ${PROJECT_SOURCE_DIR}/*.rc
  11. )

引入头文件目录


  1. #引入头文件目录
  2. include_directories(${PROJECT_SOURCE_DIR}/include)
  3. #引入头文件目录:opencv2
  4. include_directories(${PROJECT_SOURCE_DIR}/include/opencv2)

编码设置


  1. //编码设置:指定utf-8编码
  2. add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
  3. add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")

Qt 模块添加


cmake中的代码

  1. find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTSWidgets)
  2. target_link_libraries(examples_rpa PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)

Qt模块

  1. //控件类库,添加这个模块可使用Qt基本控件
  2. Widgets
  3. //网络模块,可使用这些类,QTcpSocket、QNetworkAccessManager
  4. Network
  5. //串口模块,可使用这些类,QSerialPort
  6. SerialPort
  7. //modbus库,可使用这些类,QModbusTcpClient
  8. SerialBus
  9. //该网络模块,可使用这些类,QWebEngineView
  10. WebEngineWidgets
  11. //该网络模块,可使用这些类,QMultimedia
  12. Multimedia
  13. //图表模块,可使用这些类,QChart
  14. Charts
  15. //音视频模块,视频等播放库
  16. Multimedia
  17. MultimediaWidgets
  18. //
  19. AxContainer

添加依赖库:动态库


link_libraries(链接库文件)

  • link_libraries链接库文件:方法一

  1. //这个接口需要放在"add_executable()"前面
  2. //设置库目录路径
  3. set(LIBINCLUDE_OPENCV ${PROJECT_SOURCE_DIR}/lib)
  4. //链接库文件:绝对路径
  5. link_libraries(${LIBINCLUDE_OPENCV}/opencv_img_hash452d.lib
  6. ${LIBINCLUDE_OPENCV}/opencv_world452d.lib)
  • link_libraries链接库文件:方法二

  1. //这两个接口需要放在"add_executable()"前面
  2. //设置库目录路径
  3. set(LIBINCLUDE_OPENCV ${PROJECT_SOURCE_DIR}/lib)
  4. //添加库目录
  5. link_directories(${LIBINCLUDE_OPENCV})
  6. //链接库文件:相对库目录路径
  7. link_libraries(opencv_img_hash452d.lib opencv_world452d.lib)

target_link_libraries(链接库文件,推荐使用)

  • target_link_libraries,链接库文件方法一

  1. //这6个接口需要放在"add_executable()"前面
  2. //设置库目录路径
  3. set(LIBINCLUDE_OPENCV ${PROJECT_SOURCE_DIR}/lib)
  4. //设置库目录
  5. set(LIBINCLUDE_OPENCV ${PROJECT_SOURCE_DIR}/lib)
  6. add_library(opencv_img_hash452d STATIC IMPORTED)
  7. set_property(TARGET opencv_img_hash452d PROPERTY IMPORTED_LOCATION
  8. ${LIBINCLUDE_OPENCV}/opencv_img_hash452d.lib)
  9. add_library(opencv_world452d STATIC IMPORTED)
  10. set_property(TARGET opencv_world452d PROPERTY IMPORTED_LOCATION
  11. ${LIBINCLUDE_OPENCV}/opencv_world452d.lib)
  12. //这1个接口需要放在"add_executable()"后面
  13. //链接库文件
  14. target_link_libraries(example_opencvcpp PRIVATE
  15. opencv_img_hash452d opencv_world452d
  16. )
  • target_link_libraries,链接库文件方法二

  1. //这1个接口需要放在"add_executable()"前面
  2. //设置库目录路径
  3. set(LIBINCLUDE_OPENCV ${PROJECT_SOURCE_DIR}/lib)
  4. //这1个接口需要放在"add_executable()"后面
  5. //链接库文件
  6. target_link_libraries(example_opencvcpp PRIVATE
  7. ${LIBINCLUDE_OPENCV}/opencv_img_hash452d.lib
  8. ${LIBINCLUDE_OPENCV}/opencv_world452d.lib
  9. )

全局宏


  1. //定义全局宏
  2. add_definitions(-DDEF_OPEN_MACRO)

设置变量


  1. #设置库目录路径
  2. set(LIBINCLUDE_OPENCV ${PROJECT_SOURCE_DIR}/lib)

打印日志


基本的日志

  1. //打印日志
  2. message(STATUS PROJECT_SOURCE_DIR = ${PROJECT_SOURCE_DIR})

日志:get_directory_property(获取LINK_DIRECTORIES的值)

  1. //打印日志:get_directory_property,设置的库目录
  2. get_directory_property(result LINK_DIRECTORIES)
  3. message("result: ${result}") # result: /C;/B;/A

日志:get_target_property(获取targer变量的属性)

  1. // #target_link_libraries,链接库文件方法一
  2. // ##设置库目录
  3. // #add_library(opencv_img_hash452d STATIC IMPORTED)
  4. // #set_property(TARGET opencv_img_hash452d PROPERTY IMPORTED_LOCATION
  5. // # ${LIBINCLUDE_OPENCV}/opencv_img_hash452d.lib)
  6. // #add_library(opencv_world452d STATIC IMPORTED)
  7. // #set_property(TARGET opencv_world452d PROPERTY IMPORTED_LOCATION
  8. // # ${LIBINCLUDE_OPENCV}/opencv_world452d.lib)
  9. // ##链接库文件
  10. // #target_link_libraries(example_opencvcpp PRIVATE
  11. // # opencv_img_hash452d opencv_world452d
  12. // # )
  13. //打印日志:get_directory_property(获取targer变量的属性)
  14. get_target_property(result opencv_world452d IMPORTED_LOCATION)
  15. message("result: ${result}") # result: /C;/B;/A

设置try不中断程序


  1. //设置编译参数
  2. add_definitions(/EHa)
  3. string(REGEX REPLACE "/EHsc" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})

eg1:QtWidgets Application 中的cmake


  1. cmake_minimum_required(VERSION 3.5)
  2. project(example_opencv VERSION 0.1 LANGUAGES CXX)
  3. set(CMAKE_INCLUDE_CURRENT_DIR ON)
  4. set(CMAKE_AUTOUIC ON)
  5. set(CMAKE_AUTOMOC ON)
  6. set(CMAKE_AUTORCC ON)
  7. set(CMAKE_CXX_STANDARD 17)
  8. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  9. find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
  10. find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
  11. #定义全局宏
  12. add_definitions(-DDEF_OPEN_MACRO)
  13. #编码设置:指定utf-8编码
  14. add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
  15. add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
  16. #设置库目录路径:根据debug和release模式选择
  17. if(CMAKE_BUILD_TYPE)
  18. string(TOLOWER ${CMAKE_BUILD_TYPE} BUILD_TYPE)
  19. if (${BUILD_TYPE} STREQUAL "debug")
  20. set(OPENCVLIBINCLUDE ${PROJECT_SOURCE_DIR}/../lib/x64/debug/opencv2)
  21. message("1setLibIncluce: ${BUILD_TYPE}")
  22. elseif(${BUILD_TYPE} STREQUAL "release")
  23. message("2setLibIncluce: ${BUILD_TYPE}")
  24. set(OPENCVLIBINCLUDE ${PROJECT_SOURCE_DIR}/../lib/x64/release/opencv2)
  25. else()
  26. message("3setLibIncluce: ${BUILD_TYPE}")
  27. endif()
  28. else()
  29. message("11setLibIncluce: ${CMAKE_BUILD_TYPE}")
  30. endif()
  31. #引入头文件目录
  32. include_directories(${PROJECT_SOURCE_DIR}/../Include)
  33. #引入头文件目录:opencv2
  34. include_directories(${PROJECT_SOURCE_DIR}/../Include/opencv2)
  35. #搜索源文件
  36. file(GLOB_RECURSE PROJECT_SOURCES
  37. ${PROJECT_SOURCE_DIR}/*.c
  38. ${PROJECT_SOURCE_DIR}/*.cpp
  39. ${PROJECT_SOURCE_DIR}/*.cc
  40. ${PROJECT_SOURCE_DIR}/*.h
  41. ${PROJECT_SOURCE_DIR}/*.hpp
  42. ${PROJECT_SOURCE_DIR}/*.ui
  43. ${PROJECT_SOURCE_DIR}/*.qrc
  44. ${PROJECT_SOURCE_DIR}/*.rc
  45. )
  46. #set(PROJECT_SOURCES CPP_SOURCES)
  47. if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
  48. qt_add_executable(example_opencv
  49. MANUAL_FINALIZATION
  50. ${PROJECT_SOURCES}
  51. )
  52. # Define target properties for Android with Qt 6 as:
  53. # set_property(TARGET example_opencv APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
  54. # ${CMAKE_CURRENT_SOURCE_DIR}/android)
  55. # For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
  56. else()
  57. if(ANDROID)
  58. add_library(example_opencv SHARED
  59. ${PROJECT_SOURCES}
  60. )
  61. # Define properties for Android with Qt 5 after find_package() calls as:
  62. # set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
  63. else()
  64. add_executable(example_opencv
  65. ${PROJECT_SOURCES}
  66. )
  67. endif()
  68. endif()
  69. target_link_libraries(example_opencv PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
  70. #链接库文件:根据debug和release模式选择
  71. if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  72. string(TOLOWER ${CMAKE_BUILD_TYPE} BUILD_TYPE)
  73. if (${BUILD_TYPE} STREQUAL "debug")
  74. target_link_libraries(example_opencv PRIVATE
  75. ${OPENCVLIBINCLUDE}/opencv_img_hash452d.lib
  76. ${OPENCVLIBINCLUDE}/opencv_world452d.lib
  77. )
  78. message("1target_link_libraries: ${BUILD_TYPE}")
  79. elseif(${BUILD_TYPE} STREQUAL "release")
  80. #链接库文件:target_link_libraries
  81. target_link_libraries(example_opencv PRIVATE
  82. ${OPENCVLIBINCLUDE}/opencv_img_hash452.lib
  83. ${OPENCVLIBINCLUDE}/opencv_world452.lib
  84. )
  85. message("2target_link_libraries: ${BUILD_TYPE}")
  86. else()
  87. message("3target_link_libraries: ${BUILD_TYPE}")
  88. endif()
  89. else()
  90. message("11target_link_libraries: ${CMAKE_BUILD_TYPE}")
  91. endif()
  92. set_target_properties(example_opencv PROPERTIES
  93. MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
  94. MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
  95. MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
  96. MACOSX_BUNDLE TRUE
  97. WIN32_EXECUTABLE TRUE
  98. )
  99. if(QT_VERSION_MAJOR EQUAL 6)
  100. qt_finalize_executable(example_opencv)
  101. endif()
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/658770
推荐阅读
  

闽ICP备14008679号