windows--cmake与c++的使用教程(6)

1 概述

本节目标: 了解CMAKE内置的一些变量 以及对应的值

2 CMake内置变量

  • 前面的文章中已经出现了CMAKE_CURRENT_SOURCE_DIR,当时没有说明,现在补上,CMAKE_CURRENT_SOURCE_DIR就是当前CMakeLists.txt文件所在路径。
  • 使用方法,内置变量使用, 需要用${}结合使用,比如:${CMAKE_CURRENT_SOURCE_DIR},
  • 配合message,可以查看CMake内置的一些变量的值,脚本如下
cmake_minimum_required(VERSION 3.18)

project(show_vars VERSION 1.0.1)

# 为了分行确定输出内容
message("")

message("1.PROJECT_BINARY_DIR = ${PROJECT_BINARY_DIR}")
message("2.PROJECT_SOURCE _DIR = ${_DIR}")
message("3.CMAKE_CURRENT_SOURCE_DIR = ${CMAKE_CURRENT_SOURCE_DIR}")
message("4.CMAKE_CURRRENT_BINARY_DIR = ${CMAKE_CURRRENT_BINARY_DIR}")
message("5.CMAKE_CURRENT_LIST_FILE = ${CMAKE_CURRENT_LIST_FILE}")
message("6.CMAKE_CURRENT_LIST_LINE = ${CMAKE_CURRENT_LIST_LINE}")
message("7.CMAKE_MODULE_PATH = ${CMAKE_MODULE_PATH}")
message("8.CMAKE_SOURCE_DIR = ${CMAKE_SOURCE_DIR}")
message("9.EXECUTABLE_OUTPUT_PATH = ${EXECUTABLE_OUTPUT_PATH}")
message("10.LIBRARY_OUTPUT_PATH = ${LIBRARY_OUTPUT_PATH}")
message("11.PROJECT_NAME = ${PROJECT_NAME}")
message("12.PROJECT_VERSION_MAJOR = ${PROJECT_VERSION_MAJOR}")
message("13.PROJECT_VERSION_MINOR = ${PROJECT_VERSION_MINOR}")
message("14.PROJECT_VERSION_PATCH = ${PROJECT_VERSION_PATCH}")
message("15.CMAKE_SYSTEM = ${CMAKE_SYSTEM}")
message("16.CMAKE_SYSTEM_NAME = ${CMAKE_SYSTEM_NAME}")
message("17.CMAKE_SYSTEM_VERSION = ${CMAKE_SYSTEM_VERSION}")
message("18.BUILD_SHARED_LIBS = ${BUILD_SHARED_LIBS}")
message("19.CMAKE_C_FLAGS = ${CMAKE_C_FLAGS}")
message("20.CMAKE_CXX_FLAGS = ${CMAKE_CXX_FLAGS}")
message("21.CMAKE_SYSTEM_PROCESSOR   = ${CMAKE_SYSTEM_PROCESSOR}")
# 为了分行确定输出内容
message("")

3 结果

  • 笔者机器执行脚本结果
[cmake] -- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.22621.
[cmake] 
[cmake] 1.PROJECT_BINARY_DIR = C:/A/build
[cmake] 2.PROJECT_SOURCE _DIR = 
[cmake] 3.CMAKE_CURRENT_SOURCE_DIR = C:/A
[cmake] -- Configuring done
[cmake] 4.CMAKE_CURRRENT_BINARY_DIR = 
[cmake] 5.CMAKE_CURRENT_LIST_FILE = C:/A/CMakeLists.txt
[cmake] 6.CMAKE_CURRENT_LIST_LINE = 13
[cmake] 7.CMAKE_MODULE_PATH = 
[cmake] 8.CMAKE_SOURCE_DIR = C:/A
[cmake] 9.EXECUTABLE_OUTPUT_PATH = 
[cmake] 10.LIBRARY_OUTPUT_PATH = 
[cmake] 11.PROJECT_NAME = show_vars
[cmake] 12.PROJECT_VERSION_MAJOR = 1
[cmake] 13.PROJECT_VERSION_MINOR = 0
[cmake] 14.PROJECT_VERSION_PATCH = 1
[cmake] 15.CMAKE_SYSTEM = Windows-10.0.22621
[cmake] 16.CMAKE_SYSTEM_NAME = Windows
[cmake] 17.CMAKE_SYSTEM_VERSION = 10.0.22621
[cmake] 18.BUILD_SHARED_LIBS = 
[cmake] 19.CMAKE_C_FLAGS = /DWIN32 /D_WINDOWS
[cmake] 20.CMAKE_CXX_FLAGS = /DWIN32 /D_WINDOWS /EHsc
[cmake] 21.CMAKE_SYSTEM_PROCESSOR   = AMD64
[cmake] 
[cmake] -- Generating done

5 关于

  • CMake内置的变量的值可以手动修改,并非一成不变。可根据自己项目需要,将其设置为自己需要的值。

6 关注

  • 需要重点关注的变量有CMAKE_CURRENT_SOURCE_DIRCMAKE_CURRENT_LIST_FILE
  • CMAKE_CURRENT_LIST_FILE后期自己写一些常用的脚本会用到该变量
  • CMAKE_CURRENT_SOURCE_DIR用的最多,因为它与CMakeLists.txt脚本文件相关。
posted @ 2022-11-15 23:31  mohist  阅读(216)  评论(0编辑  收藏  举报