Cmake——03add_executable生成可执行文件三种写法

文件目录如下:
image

1、传统写法:分别添加

# .h头文件
set(project_headers
        sources/headers/mainwindow.h)
# .cpp文件
set(project_cpps
        sources/main.cpp
        sources/mainwindow.cpp)
# ui文件
set(project_forms
        sources/forms/mainwindow.ui)
# 创建执行程序
add_executable(FFmpeg_qt_demo ${project_headers} ${project_cpps} ${project_forms})

2、自定义查找格式写法

这里file会根据表达式自动查找相应的文件输出给project_file变量

file(GLOB project_file
        ${PROJECT_SOURCE_DIR}/sources/headers/*.h
        ${PROJECT_SOURCE_DIR}/sources/forms/.ui
        ${PROJECT_SOURCE_DIR}/sources/*.cpp)

add_executable(FFmpeg_qt_demo ${project_file})

3、自动查找资源文件写法

使用aux_source_directory会自动查找目录下的有关c++的所有文件格式,如.cpp、.cc等,但是不会查找头文件,不需要规定文件名

set(project_headers
        sources/headers/mainwindow.h)
# ui文件
set(project_forms
        sources/forms/mainwindow.ui)

aux_source_directory(${PROJECT_SOURCE_DIR}/sources project_cFile)
add_executable(FFmpeg_qt_demo ${project_cFile} ${project_forms} ${project_headers})

实际上这里的project_cFile为一个文件列表,这里可以使用message输出一下

message(STATUS "CPP:${project_cFile}")

CPP:F:/Program/qt/FFmpeg-qt-demo/sources/main.cpp;F:/Program/qt/FFmpeg-qt-demo/sources/mainwindow.cpp
也可以使用foreach输出查看一下

foreach(c ${project_cFile})
    message(${c})
endforeach()

F:/Program/qt/FFmpeg-qt-demo/sources/main.cpp
F:/Program/qt/FFmpeg-qt-demo/sources/mainwindow.cpp

posted @ 2023-03-13 00:26  摩天仑  阅读(911)  评论(0编辑  收藏  举报