使用 CMake构建Qt项目
参考Qt官方文档
系统环境
Ubuntu 18.04
Qt5.12.2
cmake 3.10.2
构建 Qt5 的 cmake 最小版本为 3.1.0
典型的使用例子
假设有一个QtWidget项目包含一个主窗体,有 mainwindow.ui
,mainwindow.h
, mainwindow.cpp
,main.cpp
四个文件,则对应的 CMakeLists.txt
文件内容如下:
cmake_minimum_required(VERSION 3.1.0)
project(testproject)
# 在相关的构建目录中查找 include
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# 当需要的时候自动运行 moc
set(CMAKE_AUTOMOC ON)
# 从 ui 文件自动生成代码文件 ui_*.h
set(CMAKE_AUTOUIC ON)
# 查找 QtWidgets 库
find_package(Qt5Widgets CONFIG REQUIRED)
# 将源文件添加到变量中
set(helloworld_SRCS
mainwindow.ui
mainwindow.cpp
main.cpp
)
# 创建可执行程序
add_executable(helloworld ${helloword_SRCS})
# 从 Qt5 中使用 Widgets 模块
target_link_libraries(helloworld Qt5::Widgets)
如果要添加对OpenCV的支持,只需要添加:
#Op