CMake语法—调用顺序(calling sequence)
CMake语法—调用顺序(calling sequence)
1 示例代码
2 代码结构
-
learn_cmake:为根目录
-
CMakeLists.txt:CMake主脚本
-
cmake_config.bat:执行CMake配置过程的脚本(双击直接运行)
@echo off set currentDir=%~dp0 set buildDir=%currentDir% set cmakeOutputDir=%currentDir%\build cmake -S %buildDir% -B %cmakeOutputDir% -G"Visual Studio 16 2019" -T v140 -A x64 pause
-
src:工程源文件目录
-
main.cpp:源文件
-
include:工程头文件目录
-
libs:工程业务模块库
-
tools:工程业务工具库
3 代码内容
-
工程主CMakeLists.txt,即最外层的CMake脚本,也是CMake配置的入口
cmake_minimum_required(VERSION 3.18) # 设置工程名称 set(PROJECT_NAME KAIZEN) # 设置工程版本号 set(PROJECT_VERSION "1.0.0.10" CACHE STRING "默认版本号") # 工程定义 project(${PROJECT_NAME} LANGUAGES CXX C VERSION ${PROJECT_VERSION} ) # 打印开始日志 message(STATUS "########## BEGIN_TEST") include(CMakePrintHelpers) set(prefix "call_stack_prefix") cmake_print_variables(prefix PROJECT_NAME) ## 一般会先编译业务模块库 add_subdirectory(libs) ## 再编译业务工具库 add_subdirectory(tools) # 打印结束日志 message(STATUS "########## END_TEST\n")
-
libs目录下的主CMakeLists.txt,即libs目录下最外层的CMake脚本,也是CMake配置在libs目录的入口
cmake_minimum_required(VERSION 3.15) set(DIR_NAME libs) cmake_print_variables(prefix DIR_NAME) add_subdirectory(moduleA) add_subdirectory(moduleB) add_subdirectory(moduleC)
-
libs下moduleA业务模块目录的CMakeLists.txt
cmake_minimum_required(VERSION 3.15) set(TARGET_NAME moduleA) cmake_print_variables(prefix TARGET_NAME)
-
libs下moduleB业务模块目录的CMakeLists.txt
cmake_minimum_required(VERSION 3.15) set(TARGET_NAME moduleB) cmake_print_variables(prefix TARGET_NAME)
-
libs下moduleC业务模块目录的CMakeLists.txt
cmake_minimum_required(VERSION 3.15) set(TARGET_NAME moduleC) cmake_print_variables(prefix TARGET_NAME)
-
-
tools目录下的主CMakeLists.txt,即tools目录下最外层的CMake脚本,也是CMake配置在tools目录的入口
cmake_minimum_required(VERSION 3.15) set(DIR_NAME tools) cmake_print_variables(prefix DIR_NAME) add_subdirectory(toolA) add_subdirectory(toolC) add_subdirectory(toolB)
-
tools下toolA工具目录下的CMakeLists.txt
cmake_minimum_required(VERSION 3.15) set(TARGET_NAME toolA) cmake_print_variables(prefix TARGET_NAME)
-
tools下toolB工具目录下的CMakeLists.txt
cmake_minimum_required(VERSION 3.15) set(TARGET_NAME toolB) cmake_print_variables(prefix TARGET_NAME)
-
tools下toolC工具目录下的CMakeLists.txt
cmake_minimum_required(VERSION 3.15) set(TARGET_NAME toolC) cmake_print_variables(prefix TARGET_NAME)
-
-
main.cpp
int main() { return 0; }
4 运行结果
执行顺序运行结果如下:
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.17763.
-- The CXX compiler identification is MSVC 19.0.24245.0
-- The C compiler identification is MSVC 19.0.24245.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- ########## BEGIN_TEST
-- prefix="call_stack_prefix" ; PROJECT_NAME="KAIZEN"
-- prefix="call_stack_prefix" ; DIR_NAME="libs"
-- prefix="call_stack_prefix" ; TARGET_NAME="moduleA"
-- prefix="call_stack_prefix" ; TARGET_NAME="moduleB"
-- prefix="call_stack_prefix" ; TARGET_NAME="moduleC"
-- prefix="call_stack_prefix" ; DIR_NAME="tools"
-- prefix="call_stack_prefix" ; TARGET_NAME="toolA"
-- prefix="call_stack_prefix" ; TARGET_NAME="toolC"
-- prefix="call_stack_prefix" ; TARGET_NAME="toolB"
-- ########## END_TEST
-- Configuring done
-- Generating done
-- Build files have been written to: F:/learn_cmake/build
请按任意键继续. . .
5 总结
CMake配置项目时,会从主CMakeLists.txt开始配置(比如此示例中最外层的CMakeLists.txt)
接下来顺序,会严格按主CMakeLists.txt的具体写法(比如此示例中先添加libs子目录,后添加tools子目录,运行结果打印也是如此)
遇见add_subdirectory,就先对此子目录先执行配置(比如此示例中在子目录tools中,先添加toolC,后添加toolB,运行结果打印也是如此)
遵循此规律,以此类推,直至到配置完成。