cmake注释和message用法分析

时间:2022-07-09 01:08:14

cmake注释和message基础

来看一下一个简单的demo,在103message文件夹下,新建一个CMakeLists.txt,内容如下所示:

#103message/CMakeLists.txt
#[[
    message(arg1 arg2 arg3)
]]
cmake_minimum_required(VERSION 3.20)
project(message)
message("参数1") #测试message
message("参数p1" "参数p2" "p3" #[[注释在message中]] 123 test)

编译一下,可以看到输出:

参数1

参数p1参数p2p3123test

wj@ubuntu:~/xia/cmake_learning/103message$ ls
CMakeLists.txt
wj@ubuntu:~/xia/cmake_learning/103message$ cmake -S . -B build
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
参数1
参数p1参数p2p3123test
-- Configuring done
-- Generating done
-- Build files have been written to: /home/wj/xia/cmake_learning/103message/build

cmake message高级使用-指定日志级别

#103message/CMakeLists.txt
#[[
    message(arg1 arg2 arg3)
]]

#2.2 message基础使用
cmake_minimum_required(VERSION 3.20)
project(message)
message("参数1") #测试message
message("参数p1" "参数p2" "p3" #[[注释在message中]] 123 test)

#2.3 message高级使用-指定日志级别
message(FATAL_ERROR "TEST FATAL_ERROR") #这行一旦执行,camke进程就会退出
message("after FATAL_ERROR")

如下输出,可以看到程序运行到第13行,打印之后,进程就会退出。第14行并没有打印出来。

wj@ubuntu:~/xia/cmake_learning/103message$ ls
CMakeLists.txt
wj@ubuntu:~/xia/cmake_learning/103message$ cmake -S . -B build
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
参数1
参数p1参数p2p3123test
CMake Error at CMakeLists.txt:13 (message):
  TEST FATAL_ERROR


-- Configuring incomplete, errors occurred!
See also "/home/wj/xia/cmake_learning/103message/build/CMakeFiles/CMakeOutput.log".

如果是改成 SEND_ERROR。cmake进程并不会停止,而是会继续往下执行。可以看到 after FATAL_ERROR 是有输出的。

#103message/CMakeLists.txt
#[[
    message(arg1 arg2 arg3)
]]

#2.2 message基础使用
cmake_minimum_required(VERSION 3.20)
project(message)
message("参数1") #测试message
message("参数p1" "参数p2" "p3" #[[注释在message中]] 123 test)

#2.3 message高级使用-指定日志级别
#message(FATAL_ERROR "TEST FATAL_ERROR") #这行一旦执行,camke进程就会退出

message(SEND_ERROR "TEST SEND_ERROR") #进程继续
message("after FATAL_ERROR")

wj@ubuntu:~/xia/cmake_learning/103message$ ls
CMakeLists.txt
wj@ubuntu:~/xia/cmake_learning/103message$ cmake -S . -B build
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
参数1
参数p1参数p2p3123test
CMake Error at CMakeLists.txt:15 (message):
  TEST SEND_ERROR


after FATAL_ERROR
-- Configuring incomplete, errors occurred!
See also "/home/wj/xia/cmake_learning/103message/build/CMakeFiles/CMakeOutput.log".

修改项目,增加 test_message.cpp

#include <iostream>

using namespace std;

int main(int argc,char* argv[])
{
    cout<<"test message"<<endl;

    return 0;
}

CMakeLists.txt

#103message/CMakeLists.txt
#[[
    message(arg1 arg2 arg3)
]]

#2.2 message基础使用
cmake_minimum_required(VERSION 3.20)
project(message)
message("参数1") #测试message
message("参数p1" "参数p2" "p3" #[[注释在message中]] 123 test)

#2.3 message高级使用-指定日志级别
#message(FATAL_ERROR "TEST FATAL_ERROR") #这行一旦执行,camke进程就会退出

message(SEND_ERROR "TEST SEND_ERROR") #进程继续

add_executable(test_message test_message.cpp)

message("after ERROR")
message(WARNING "TEST WARNING")

wj@ubuntu:~/xia/cmake_learning/103message$ ls
CMakeLists.txt  test_message.cpp
wj@ubuntu:~/xia/cmake_learning/103message$ cmake -S . -B build
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
参数1
参数p1参数p2p3123test
CMake Error at CMakeLists.txt:15 (message):
  TEST SEND_ERROR


after ERROR
CMake Warning at CMakeLists.txt:20 (message):
  TEST WARNING


-- Configuring incomplete, errors occurred!
See also "/home/wj/xia/cmake_learning/103message/build/CMakeFiles/CMakeOutput.log".

cmake -S . -B build ==> 默认不显示 VERBOSE log的。

cmake -S . -B build --log-level=VERBOSE ==> 这样会显示 VERBOSE 的log

#103message/CMakeLists.txt
#[[
    message(arg1 arg2 arg3)
]]

#2.2 message基础使用
cmake_minimum_required(VERSION 3.20)
project(message)
message("参数1") #测试message
message("参数p1" "参数p2" "p3" #[[注释在message中]] 123 test)

#2.3 message高级使用-指定日志级别
#message(FATAL_ERROR "TEST FATAL_ERROR") #这行一旦执行,camke进程就会退出

message(SEND_ERROR "TEST SEND_ERROR") #进程继续

add_executable(test_message test_message.cpp)

message("after ERROR")
message(WARNING "TEST WARNING")

#NOTICE等同于 none 也就是不加 message("TEST NOTICE")
message(NOTICE "TEST NOTICE")

#STATUS 加前缀 -- 用户可能感兴趣
message(STATUS "TEST STATUS")

#VERBOSE 默认不显示 用户需要的纤细信息
message(VERBOSE "TEST VERBOSE")

#设置日志显示级别
#cmake -S . -B build  --log-level=VERBOSE

#标准输出重定向到文件log.txt

cmake -S . -B build --log-level=VERBOSE > log.txt

#标准错误输出重定向到标准输出

cmake -S . -B build --log-level=VERBOSE > log.txt 2>&1

cmake -S . -B build --log-level=DEBUG

cmake -S . -B build --log-level=TRACE

#103message/CMakeLists.txt
#[[
    message(arg1 arg2 arg3)
]]

#2.2 message基础使用
cmake_minimum_required(VERSION 3.20)
project(message)
message("参数1") #测试message
message("参数p1" "参数p2" "p3" #[[注释在message中]] 123 test)

#2.3 message高级使用-指定日志级别
#message(FATAL_ERROR "TEST FATAL_ERROR") #这行一旦执行,camke进程就会退出

message(SEND_ERROR "TEST SEND_ERROR") #进程继续

add_executable(test_message test_message.cpp)

message("after ERROR")
message(WARNING "TEST WARNING")

#NOTICE等同于 none 也就是不加 message("TEST NOTICE")
message(NOTICE "TEST NOTICE")

#STATUS 加前缀 -- 用户可能感兴趣
message(STATUS "TEST STATUS")

#VERBOSE 默认不显示 用户需要的纤细信息
message(VERBOSE "TEST VERBOSE")

#设置日志显示级别
#cmake -S . -B build  --log-level=VERBOSE

#标准输出重定向到文件log.txt
#cmake -S . -B build --log-level=VERBOSE > log.txt

#标准错误输出重定向到标准输出
#cmake -S . -B build --log-level=VERBOSE > log.txt 2>&1


#DEBUG 加前缀 --
#cmake -S . -B build --log-level=DEBUG
message(DEBUG "test DEBUG")

#TRACE 加前缀 --
#cmake -S . -B build --log-level=TRACE
message(TRACE "test TRACE")

cmake message 模块查找日志和显示缩进

CMakeLists.txt

#103message/CMakeLists.txt
#[[
    message(arg1 arg2 arg3)
]]

#2.2 message基础使用
cmake_minimum_required(VERSION 3.20)
project(message)
message("参数1") #测试message
message("参数p1" "参数p2" "p3" #[[注释在message中]] 123 test)

#2.3 message高级使用-指定日志级别
#message(FATAL_ERROR "TEST FATAL_ERROR") #这行一旦执行,camke进程就会退出

message(SEND_ERROR "TEST SEND_ERROR") #进程继续

add_executable(test_message test_message.cpp)

message("after ERROR")
message(WARNING "TEST WARNING")

#NOTICE等同于 none 也就是不加 message("TEST NOTICE")
message(NOTICE "TEST NOTICE")

#STATUS 加前缀 -- 用户可能感兴趣
message(STATUS "TEST STATUS")

#VERBOSE 默认不显示 用户需要的纤细信息
message(VERBOSE "TEST VERBOSE")

#设置日志显示级别
#cmake -S . -B build  --log-level=VERBOSE

#标准输出重定向到文件log.txt
#cmake -S . -B build --log-level=VERBOSE > log.txt

#标准错误输出重定向到标准输出
#cmake -S . -B build --log-level=VERBOSE > log.txt 2>&1


#DEBUG 加前缀 --
#cmake -S . -B build --log-level=DEBUG
message(DEBUG "test DEBUG")

#TRACE 加前缀 --
#cmake -S . -B build --log-level=TRACE
message(TRACE "test TRACE")

#2.4 message Reporting checks 查找库日志
#[[
    CHECK_START 开始记录将要执行检查的信息
    CHECK_PASS 记录检查的成功结果
    CHECK_FAIL 记录不成功的检查结果
]]
message("======================================================= 2.4 ====================================================")
#开始查找
message(CHECK_START "Find xcpp")
#查找库的代码
message(CHECK_FAIL "Not found") 

wj@ubuntu:~/xia/cmake_learning/103message$ ls
CMakeLists.txt  log.txt  test_message.cpp
wj@ubuntu:~/xia/cmake_learning/103message$ cmake -S . -B build

wj@ubuntu:~/xia/cmake_learning/103message$ cmake -S . -B build
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
参数1
参数p1参数p2p3123test
CMake Error at CMakeLists.txt:15 (message):
  TEST SEND_ERROR


after ERROR
CMake Warning at CMakeLists.txt:20 (message):
  TEST WARNING


TEST NOTICE
-- TEST STATUS
======================================================= 2.4 ====================================================
-- Find xcpp
-- Find xcpp - Not found
-- Configuring incomplete, errors occurred!
See also "/home/wj/xia/cmake_learning/103message/build/CMakeFiles/CMakeOutput.log".

CMakeLists.txt

#103message/CMakeLists.txt
#[[
    message(arg1 arg2 arg3)
]]

#2.2 message基础使用
cmake_minimum_required(VERSION 3.20)
project(message)
message("参数1") #测试message
message("参数p1" "参数p2" "p3" #[[注释在message中]] 123 test)

#2.3 message高级使用-指定日志级别
#message(FATAL_ERROR "TEST FATAL_ERROR") #这行一旦执行,camke进程就会退出

message(SEND_ERROR "TEST SEND_ERROR") #进程继续

add_executable(test_message test_message.cpp)

message("after ERROR")
message(WARNING "TEST WARNING")

#NOTICE等同于 none 也就是不加 message("TEST NOTICE")
message(NOTICE "TEST NOTICE")

#STATUS 加前缀 -- 用户可能感兴趣
message(STATUS "TEST STATUS")

#VERBOSE 默认不显示 用户需要的纤细信息
message(VERBOSE "TEST VERBOSE")

#设置日志显示级别
#cmake -S . -B build  --log-level=VERBOSE

#标准输出重定向到文件log.txt
#cmake -S . -B build --log-level=VERBOSE > log.txt

#标准错误输出重定向到标准输出
#cmake -S . -B build --log-level=VERBOSE > log.txt 2>&1


#DEBUG 加前缀 --
#cmake -S . -B build --log-level=DEBUG
message(DEBUG "test DEBUG")

#TRACE 加前缀 --
#cmake -S . -B build --log-level=TRACE
message(TRACE "test TRACE")

#2.4 message Reporting checks 查找库日志
#[[
    CHECK_START 开始记录将要执行检查的信息
    CHECK_PASS 记录检查的成功结果
    CHECK_FAIL 记录不成功的检查结果
]]
message("======================================================= 2.4 ====================================================")
#开始查找
message(CHECK_START "查找xcpp")
#查找xcpp的代码

#嵌套查找
message(CHECK_START "查找xlog")
#查找xlog的代码


#message(CHECK_PASS "成功")




#查找库的代码
message(CHECK_FAIL "失败") 

wj@ubuntu:~/xia/cmake_learning/103message$ cmake -S . -B build
参数1
参数p1参数p2p3123test
CMake Error at CMakeLists.txt:15 (message):
  TEST SEND_ERROR


after ERROR
CMake Warning at CMakeLists.txt:20 (message):
  TEST WARNING


TEST NOTICE
-- TEST STATUS
======================================================= 2.4 ====================================================
-- 查找xcpp
-- 查找xlog
-- 查找xlog - 失败
-- Configuring incomplete, errors occurred!
See also "/home/wj/xia/cmake_learning/103message/build/CMakeFiles/CMakeOutput.log".

CMakeLists.txt

#103message/CMakeLists.txt
#[[
    message(arg1 arg2 arg3)
]]

#2.2 message基础使用
cmake_minimum_required(VERSION 3.20)
project(message)
message("参数1") #测试message
message("参数p1" "参数p2" "p3" #[[注释在message中]] 123 test)

#2.3 message高级使用-指定日志级别
#message(FATAL_ERROR "TEST FATAL_ERROR") #这行一旦执行,camke进程就会退出

message(SEND_ERROR "TEST SEND_ERROR") #进程继续

add_executable(test_message test_message.cpp)

message("after ERROR")
message(WARNING "TEST WARNING")

#NOTICE等同于 none 也就是不加 message("TEST NOTICE")
message(NOTICE "TEST NOTICE")

#STATUS 加前缀 -- 用户可能感兴趣
message(STATUS "TEST STATUS")

#VERBOSE 默认不显示 用户需要的纤细信息
message(VERBOSE "TEST VERBOSE")

#设置日志显示级别
#cmake -S . -B build  --log-level=VERBOSE

#标准输出重定向到文件log.txt
#cmake -S . -B build --log-level=VERBOSE > log.txt

#标准错误输出重定向到标准输出
#cmake -S . -B build --log-level=VERBOSE > log.txt 2>&1


#DEBUG 加前缀 --
#cmake -S . -B build --log-level=DEBUG
message(DEBUG "test DEBUG")

#TRACE 加前缀 --
#cmake -S . -B build --log-level=TRACE
message(TRACE "test TRACE")

#2.4 message Reporting checks 查找库日志
#[[
    CHECK_START 开始记录将要执行检查的信息
    CHECK_PASS 记录检查的成功结果
    CHECK_FAIL 记录不成功的检查结果
]]
message("======================================================= 2.4 ====================================================")
#开始查找
message(CHECK_START "查找xcpp")
#查找xcpp的代码

#嵌套查找
message(CHECK_START "查找xlog")
#查找xlog的代码
message(CHECK_PASS "成功")




#查找库的代码
message(CHECK_FAIL "失败")

wj@ubuntu:~/xia/cmake_learning/103message$ cmake -S . -B build
参数1
参数p1参数p2p3123test
CMake Error at CMakeLists.txt:15 (message):
  TEST SEND_ERROR


after ERROR
CMake Warning at CMakeLists.txt:20 (message):
  TEST WARNING


TEST NOTICE
-- TEST STATUS
======================================================= 2.4 ====================================================
-- 查找xcpp
-- 查找xlog
-- 查找xlog - 成功
-- 查找xcpp - 失败
-- Configuring incomplete, errors occurred!
See also "/home/wj/xia/cmake_learning/103message/build/CMakeFiles/CMakeOutput.log".

CMakeLists.txt

#103message/CMakeLists.txt
#[[
    message(arg1 arg2 arg3)
]]

#2.2 message基础使用
cmake_minimum_required(VERSION 3.20)
project(message)
message("参数1") #测试message
message("参数p1" "参数p2" "p3" #[[注释在message中]] 123 test)

#2.3 message高级使用-指定日志级别
#message(FATAL_ERROR "TEST FATAL_ERROR") #这行一旦执行,camke进程就会退出

message(SEND_ERROR "TEST SEND_ERROR") #进程继续

add_executable(test_message test_message.cpp)

message("after ERROR")
message(WARNING "TEST WARNING")

#NOTICE等同于 none 也就是不加 message("TEST NOTICE")
message(NOTICE "TEST NOTICE")

#STATUS 加前缀 -- 用户可能感兴趣
message(STATUS "TEST STATUS")

#VERBOSE 默认不显示 用户需要的纤细信息
message(VERBOSE "TEST VERBOSE")

#设置日志显示级别
#cmake -S . -B build  --log-level=VERBOSE

#标准输出重定向到文件log.txt
#cmake -S . -B build --log-level=VERBOSE > log.txt

#标准错误输出重定向到标准输出
#cmake -S . -B build --log-level=VERBOSE > log.txt 2>&1


#DEBUG 加前缀 --
#cmake -S . -B build --log-level=DEBUG
message(DEBUG "test DEBUG")

#TRACE 加前缀 --
#cmake -S . -B build --log-level=TRACE
message(TRACE "test TRACE")

#2.4 message Reporting checks 查找库日志
#[[
    CHECK_START 开始记录将要执行检查的信息
    CHECK_PASS 记录检查的成功结果
    CHECK_FAIL 记录不成功的检查结果
]]
message("======================================================= 2.4 ====================================================")
#开始查找
message(CHECK_START "查找xcpp")
#查找xcpp的代码

#嵌套查找
message(CHECK_START "查找xlog")
#查找xlog的代码
message(CHECK_PASS "成功")

message(CHECK_START "查找xthreadpool")
message(CHECK_FAIL "失败")

#查找库的代码
message(CHECK_FAIL "失败")

wj@ubuntu:~/xia/cmake_learning/103message$ cmake -S . -B build
参数1
参数p1参数p2p3123test
CMake Error at CMakeLists.txt:15 (message):
  TEST SEND_ERROR


after ERROR
CMake Warning at CMakeLists.txt:20 (message):
  TEST WARNING


TEST NOTICE
-- TEST STATUS
======================================================= 2.4 ====================================================
-- 查找xcpp
-- 查找xlog
-- 查找xlog - 成功
-- 查找xthreadpool
-- 查找xthreadpool - 失败
-- 查找xcpp - 失败
-- Configuring incomplete, errors occurred!
See also "/home/wj/xia/cmake_learning/103message/build/CMakeFiles/CMakeOutput.log".

message消息缩进用法:

CMakeLists.txt

#103message/CMakeLists.txt
#[[
    message(arg1 arg2 arg3)
]]

#2.2 message基础使用
cmake_minimum_required(VERSION 3.20)
project(message)
message("参数1") #测试message
message("参数p1" "参数p2" "p3" #[[注释在message中]] 123 test)

#2.3 message高级使用-指定日志级别
#message(FATAL_ERROR "TEST FATAL_ERROR") #这行一旦执行,camke进程就会退出

message(SEND_ERROR "TEST SEND_ERROR") #进程继续

add_executable(test_message test_message.cpp)

message("after ERROR")
message(WARNING "TEST WARNING")

#NOTICE等同于 none 也就是不加 message("TEST NOTICE")
message(NOTICE "TEST NOTICE")

#STATUS 加前缀 -- 用户可能感兴趣
message(STATUS "TEST STATUS")

#VERBOSE 默认不显示 用户需要的纤细信息
message(VERBOSE "TEST VERBOSE")

#设置日志显示级别
#cmake -S . -B build  --log-level=VERBOSE

#标准输出重定向到文件log.txt
#cmake -S . -B build --log-level=VERBOSE > log.txt

#标准错误输出重定向到标准输出
#cmake -S . -B build --log-level=VERBOSE > log.txt 2>&1


#DEBUG 加前缀 --
#cmake -S . -B build --log-level=DEBUG
message(DEBUG "test DEBUG")

#TRACE 加前缀 --
#cmake -S . -B build --log-level=TRACE
message(TRACE "test TRACE")

#2.4 message Reporting checks 查找库日志
#[[
    CHECK_START 开始记录将要执行检查的信息
    CHECK_PASS 记录检查的成功结果
    CHECK_FAIL 记录不成功的检查结果
]]
message("======================================================= 2.4 ====================================================")
#开始查找
message(CHECK_START "查找xcpp")
#查找xcpp的代码

# message消息缩进
set(CMAKE_MESSAGE_INDENT "--")

#嵌套查找
message(CHECK_START "查找xlog")
#查找xlog的代码
message(CHECK_PASS "成功")

message(CHECK_START "查找xthreadpool")
message(CHECK_FAIL "失败")


#取消缩进
set(CMAKE_MESSAGE_INDENT "")

#查找库的代码
message(CHECK_FAIL "失败")

wj@ubuntu:~/xia/cmake_learning/103message$ cmake -S . -B build
参数1
参数p1参数p2p3123test
CMake Error at CMakeLists.txt:15 (message):
  TEST SEND_ERROR


after ERROR
CMake Warning at CMakeLists.txt:20 (message):
  TEST WARNING


TEST NOTICE
-- TEST STATUS
======================================================= 2.4 ====================================================
-- 查找xcpp
-- --查找xlog
-- --查找xlog - 成功
-- --查找xthreadpool
-- --查找xthreadpool - 失败
-- 查找xcpp - 失败
-- Configuring incomplete, errors occurred!
See also "/home/wj/xia/cmake_learning/103message/build/CMakeFiles/CMakeOutput.log".

有缩进的话,代码目录更加直观一些:代表xlog xthreadpool 是xcpp的子模块目录。