CMake学习

目录

一 各种CMake命令

二 CMake 内部变量

三 cmake调用第三方库


一 各种CMake命令

1.find_package

 官方文档:find_package — CMake 3.5.2 Documentation

Qt为例找到相应的Qt5依赖库,新建一个CMakeList.txt文件,写入:

cmake_minimum_required(VERSION 3.10)

#设置工程名CMakeFindQt 
project(CMakeFindQt VERSION 0.1.0)

#指定C++ 14 支持
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED on)

#CMAKE_INCLUDE_CURRENT_DIR  默认是关,包含当前目录
set(CMAKE_INCLUDE_CURRENT_DIR ON)

#搜索Qt5 安装包
find_package(Qt5 REQUIRED)

  其中REQUIRED :The REQUIRED option stops processing with an error message if the package cannot be found.

运行 find_package(Qt5 REQUIRED ) 会报错:
CMake Error at CMakeLists.txt:44 (find_package):
By not providing "FindQt5.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "Qt5", but CMake did not find one.
Could not find a package configuration file provided by "Qt5" with any of the following names:
        Qt5Config.cmake
        qt5-config.cmake
Add the installation prefix of "Qt5" to CMAKE_PREFIX_PATH or set "Qt5_DIR" to a directory containing one of the above files. If "Qt5" provides a separate development package or SDK, be sure it has been installed.

错误中其实给出了解决方案:

可以增加:set(Qt_DIR "F:/SoftwareInstalling/Qt/Qt5.13.0/5.13.0/msvc2017/lib/cmake/Qt5")(注意路径中的‘/’)

"F:/SoftwareInstalling/Qt/Qt5.13.0/5.13.0/msvc2017/lib/cmake/Qt5"目录下存在“Qt5Config.cmake”文件。

还可以在cmake-gui中指定Qt库cmke路径:

之后“configure”继续报错:

CMake Error at CMakeLists.txt: (find_package):
Found package configuration file:
F:/SoftwareInstalling/Qt/Qt5.13.0/5.13.0/msvc2017/lib/cmake/Qt5/Qt5Config.cmake
but it set Qt5_FOUND to FALSE so package "Qt5" is considered to be NOT FOUND. Reason given by package:
The Qt5 package requires at least one component

在错误信息中看出需要指定至少一个component,修改find_package():

find_package(Qt5 REQUIRED COMPONENTS Core Gui Widgets Network Multimedia SerialPort Sql Xml)

继续“configure”结果如下:

CMake学习_第1张图片

添加Qt5依赖库:

target_link_libraries(${PROJECT_NAME} Qt5::Widgets Qt5::Core Qt5::Gui)
target_link_libraries(${PROJECT_NAME} Qt5::Network Qt5::Sql Qt5::Xml)
target_link_libraries(${PROJECT_NAME} Qt5::SerialPort Qt5::Qt5Multimedia)

完整的Qt5指定如下:

cmake_minimum_required(VERSION 3.10)

#设置工程名CMakeFindQt 
project(CMakeFindQt VERSION 0.1.0)

#指定C++ 14 支持
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED on)

#CMAKE_INCLUDE_CURRENT_DIR  默认是关,包含当前目录
set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(Qt_DIR "F:/SoftwareInstalling/Qt/Qt5.13.0/5.13.0/msvc2017/lib/cmake/Qt5")

#搜索Qt5 安装包
find_package(Qt5 REQUIRED COMPONENTS Core Gui Widgets Network Multimedia SerialPort Sql Xml)

target_link_libraries(${PROJECT_NAME} Qt5::Widgets Qt5::Core Qt5::Gui)
target_link_libraries(${PROJECT_NAME} Qt5::Network Qt5::Sql Qt5::Xml)
target_link_libraries(${PROJECT_NAME} Qt5::SerialPort Qt5::Qt5Multimedia)

2.add_definitions()

添加宏定义

add_definitins(-D_DEBUG) 等价于命令中: cmake  -D_DEBUG=1

在下面代码中起作用:

#ifdef _DEBUG
     std::cout<<"_DEBUG is defined"<

2.file()

file — CMake 3.12.4 Documentation

例子:FILE(GLOB_RECURSE SRC_FILES "./src/*.cpp")

GLOB_RECURSE 表示使用通配符 ,所有的“./src/*.cpp”文件都包含在SRC_FILES 中。

可以调用source_group("Src Files"  FILES ${SRC_FILES} )在工程中将.cpp 放到“Src Files”组下。

3.add_library()

add_library — CMake 3.12.4 Documentation

add_library(archive archive.cpp zip.cpp lzma.cpp) 创建一个名为archive动态库

4.source_group()

source_group — CMake 3.19.8 Documentation

把文档归类

5.关键字  PRIVATE PUBLIC INTERFACE

CMake编译中target_link_libraries中属性PRIVATE、PUBLIC、INTERFACE含义_Turbock的博客-CSDN博客_cmake interface

6.cmake 链接qt4

官方教程:Using CMake build system - Qt Wiki

7.cmake查看各种内部变量

CMake之常用内置变量和message用法 - mohist - 博客园

二 CMake 内部变量

(1)CMAKE_INCLUDE_CURRENT_DIR

CMAKE_INCLUDE_CURRENT_DIR — CMake 3.0.2 Documentation

指定包含当前路径,默认是关闭的,打开操作:set(CMAKE_INCLUDE_CURRENT_DIR ON)

(2)cmake指定使用C++11

在CMakeLists.txt中添加:add_definitions(-std=c++11)

使用C++17 添加:set(CMAKE_CXX_STANDARD 17)

两行语句要放到CMakeLists.txt文件开头部分,否则无效。

set — CMake 3.0.2 Documentation set参考文档

三 cmake调用第三方库

cmake配置项目引用动态库 - mohist - 博客园

cmake快速入门_kavin.zhu的博客-CSDN博客_cmake

参考:cmake 常用变量、常用环境变量、常用语法总结_bytxl的专栏-CSDN博客_cmake 变量https://blog.csdn.net/bytxl/article/details/50634868

你可能感兴趣的:(CMake,Learn,cmake,C++)