Qt上CMakeLists的应用
参考来源 SFUMECJF/cmake-examples-Chinese
# Set the minimum version of CMake that can be used
#设置CMake最小版本
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.5)
# Set the project name
#设置工程名
project (reconstruction)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_PREFIX_PATH "D:/Program/Qt/6.4.0/msvc2019_64/lib/cmake")#为了不在Qt creator这个软件中也能CMake找到Qt的库
find_package(Qt6 COMPONENTS Widgets REQUIRED)
find_package(Qt6 COMPONENTS Core REQUIRED)
#find_package(pthread REQUIRED)
# Add an executable
#生成可执行文件
file(GLOB_RECURSE SRC_FILES
source/*.cpp
source/*.ui
)
file(GLOB_RECURSE INC_FILES
include/*.h
# include/*.ui
)
add_executable(hello_cmake main.cpp ${SRC_FILES} ${INC_FILES})
# add_executable(hello_cmake main.cpp include/mainwindow.h source/mainwindow.cpp source/mainwindow.ui)
#
target_link_libraries(hello_cmake Qt6::Core Qt6::Widgets)
target_include_directories(hello_cmake PRIVATE include/)
# 注意include后面要加 斜杠 / 表示是一个目录
# Set the minimum version of CMake that can be used
#设置CMake最小版本
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.5)
# Set the project name
#设置工程名
project (reconstruction)
#注意,在一个大工程里,这几个开关一定要尽量放在前面打开,否则有可能会报无法生成ui文件的错误
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
#注意,在一个大工程里,这几个开关一定要尽量放在前面打开,否则有可能会报无法生成ui文件的错误
set(CMAKE_PREFIX_PATH "D:/Program/Qt/6.4.0/msvc2019_64/lib/cmake")#为了不在Qt creator这个软件中也能CMake找到Qt的库
find_package(Qt6 COMPONENTS Widgets REQUIRED)
#get_target_property(QtCore_location Qt5::Widgets LOCATION)
find_package(Qt6 COMPONENTS Core REQUIRED)
#get_target_property(QtCore_location Qt5::Core LOCATION)
#find_package(pthread REQUIRED)
# Add an executable
#生成可执行文件
file(GLOB_RECURSE SRC_FILES
source/*.cpp
source/*.ui
)
file(GLOB_RECURSE INC_FILES
include/*.h
# include/*.ui
)
add_executable(hello_cmake main.cpp ${SRC_FILES} ${INC_FILES})
# add_executable(hello_cmake main.cpp include/mainwindow.h source/mainwindow.cpp source/mainwindow.ui)
#
target_link_libraries(hello_cmake Qt6::Core Qt6::Widgets)
target_include_directories(hello_cmake PRIVATE include/)
#在这里,为目标链接了/usr/lib/x86_64-linux-gnu/libpthread.so
#如果不链接就会报错,这是系统里的库,直接链接就行。
#如果是用的.pro文件,则在里面加上LIBS+=/usr/lib/x86_64-linux-gnu/libpthread.so
#target_link_libraries(hello_cmake pthread)