1.概要
创建工程的时候,如果每个文件都手动写入到工程中很麻烦,如下
add_executable(untitled4
main.cpp
a.h a.cpp
)
上面的a.h a.cpp有多少文件就添多少,如果文件多了很麻烦,其实可以用cmke提供的file函数解决这个问题,如下:
file (GLOB MAIN_SRC
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
)
file (GLOB MAIN_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/*.h
)
add_executable(untitled4
main.cpp
${MAIN_SRC} ${MAIN_HEADERS}
)
2.代码
1.工程文件

2.A头文件
#ifndef A_H
#define A_H
class A
{
public:
A();
};
#endif // A_H
3.A源文件
#include "a.h"
#include <QDebug>
A::A() {
qDebug()<<"a structure";
}
4.主函数
#include <QCoreApplication>
#include <QDebug>
#include "a.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
A a1;
qDebug()<<"ddd";
return a.exec();
}
5.工程文件
1.自动生成的
cmake_minimum_required(VERSION 3.14)
project(untitled4 LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)
add_executable(untitled4
main.cpp
a.h a.cpp
)
target_link_libraries(untitled4 Qt${QT_VERSION_MAJOR}::Core)
include(GNUInstallDirs)
install(TARGETS untitled4
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
2.我改的
cmake_minimum_required(VERSION 3.14)
project(untitled4 LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)
file (GLOB MAIN_SRC
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
)
file (GLOB MAIN_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/*.h
)
add_executable(untitled4
main.cpp
${MAIN_SRC} ${MAIN_HEADERS}
)
target_link_libraries(untitled4 Qt${QT_VERSION_MAJOR}::Core)
include(GNUInstallDirs)
install(TARGETS untitled4
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
3.运行结果
09:11:03: Debugging D:\projects\qts\untitled4\build\Desktop_Qt_6_7_2_MinGW_64_bit-Debug\untitled4.exe ...
a structure
ddd
3029

被折叠的 条评论
为什么被折叠?



