7.5 重新定义函数和宏
准备工作
.
├── cmake
│ ├── custom.cmake
│ └── include_guard.cmake
└── CMakeLists.txtinclude_guard(GLOBAL)
message(STATUS "custom.cmake is included and processed")具体实施
cmake_minimum_required(VERSION 3.5 FATAL_ERROR) project(recipe-05 LANGUAGES NONE)# (re)defines include_guard include(cmake/include_guard.cmake)macro(include_guard) if (CMAKE_VERSION VERSION_LESS "3.10") # for CMake below 3.10 we define our # own include_guard(GLOBAL) message(STATUS "calling our custom include_guard") # if this macro is called the first time # we start with an empty list if(NOT DEFINED included_modules) set(included_modules) endif() if ("${CMAKE_CURRENT_LIST_FILE}" IN_LIST included_modules) message(WARNING "module ${CMAKE_CURRENT_LIST_FILE} processed more than once") endif() list(APPEND included_modules ${CMAKE_CURRENT_LIST_FILE}) else() # for CMake 3.10 or higher we augment # the built-in include_guard message(STATUS "calling the built-in include_guard") _include_guard(${ARGV}) endif() endmacro()include(cmake/custom.cmake) include(cmake/custom.cmake)$ mkdir -p build $ cd build $ cmake ..-- calling the built-in include_guard -- custom.cmake is included and processed -- calling the built-in include_guard- calling our custom include_guard -- custom.cmake is included and processed -- calling our custom include_guard CMake Warning at cmake/include_guard.cmake:7 (message): module /home/user/example/cmake/custom.cmake processed more than once Call Stack (most recent call first): cmake/custom.cmake:1 (include_guard) CMakeLists.txt:12 (include)
工作原理
Last updated