cmake add_subdirectory
时间: 2023-03-19 19:30:02 浏览: 195
cmake add_subdirectory是CMake中的一个命令,用于向CMake项目中添加子目录。通过这个命令,可以将一个子目录添加到当前项目中,并在子目录中执行CMakeLists.txt文件中的命令。这个命令通常用于将一个较大的项目分解成多个子项目,以便更好地管理和维护代码。
相关问题
cmake_minimum_required(VERSION 3.8.0 FATAL_ERROR) project(mcu_kit VERSION 0.1.0 DESCRIPTION "mcu-kit SDK" LANGUAGES C ) set(PROJECT_BRIEF "vDiscovery") set(CMAKE_C_STANDARD 11) set(CMAKE_C_EXTENSIONS ON) set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD 11) set(CMAKE_POSITION_INDEPENDENT_CODE ON) add_compile_options(-Wall) list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") include(CMakePrintHelpers) include(ConfigureChecks) if(EXISTS "${PROJECT_SOURCE_DIR}/config.cmake") include(config.cmake) else() message(FATAL_ERROR "Can't find cmake.config file") endif() if(${CMAKE_CROSSCOMPILING}) set(LV_SIMULATOR_ON_PC OFF) else() set(LV_SIMULATOR_ON_PC ON) endif() configure_file( "${PROJECT_SOURCE_DIR}/base/base_sdk_version.h.in" "${PROJECT_SOURCE_DIR}/base/base_sdk_version.h" ) configure_file( "${PROJECT_SOURCE_DIR}/base/base_config.h.in" "${PROJECT_SOURCE_DIR}/base/base_config.h" ) set(CMAKE_INSTALL_BINDIR ${CMAKE_INSTALL_PREFIX}/bin) set(CMAKE_INSTALL_SBINDIR ${CMAKE_INSTALL_PREFIX}/sbin) set(CMAKE_INSTALL_LIBDIR ${CMAKE_INSTALL_PREFIX}/lib) set(CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX}/include) set(CMAKE_INSTALL_SYSCONFDIR ${CMAKE_INSTALL_PREFIX}/etc) set(CMAKE_INSTALL_SHARESTATEDIR ${CMAKE_INSTALL_PREFIX}/com) set(CMAKE_INSTALL_LOCALSTATEDIR ${CMAKE_INSTALL_PREFIX}/var) set(CMAKE_INSTALL_INFODIR ${CMAKE_INSTALL_PREFIX}/info) set(CMAKE_INSTALL_LOCALEDIR ${CMAKE_INSTALL_PREFIX}/locate) set(CMAKE_INSTALL_MANDIR ${CMAKE_INSTALL_PREFIX}/man) set(CMAKE_INSTALL_DOCDIR ${CMAKE_INSTALL_PREFIX}/doc) set(CMAKE_INSTALL_RESDIR ${CMAKE_INSTALL_PREFIX}) include(PrepareInstall) set(KIT_ROOT_DIR ${CMAKE_SOURCE_DIR}) add_subdirectory("base") # add_subdirectory("third-party") add_subdirectory("lib") add_subdirectory("drivers") add_subdirectory("framework") add_subdirectory("sysapp") add_subdirectory("app") include(GeneratePkgConfig) include(Linux-kitInfo)详细注释一下这段代码
这段代码是一个 CMakeLists.txt 文件,用于构建一个名为 "mcu_kit" 的项目。下面是对每行代码的详细注释:
```cmake
cmake_minimum_required(VERSION 3.8.0 FATAL_ERROR)
```
指定最低的 CMake 版本,如果当前版本不符合要求,则提示错误。
```cmake
project(mcu_kit VERSION 0.1.0 DESCRIPTION "mcu-kit SDK" LANGUAGES C)
```
定义项目名称、版本、描述和使用的编程语言。
```cmake
set(PROJECT_BRIEF "vDiscovery")
```
设置一个变量,用于描述项目简介。
```cmake
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_EXTENSIONS ON)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 11)
```
指定使用的 C 和 C++ 标准版本。
```cmake
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
```
启用位置独立代码(PIC)选项。
```cmake
add_compile_options(-Wall)
```
添加编译选项,这里添加了 -Wall 选项,表示开启所有警告。
```cmake
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
```
将自定义的 CMake 模块路径添加到 CMAKE_MODULE_PATH 变量中。
```cmake
include(CMakePrintHelpers)
include(ConfigureChecks)
```
包含两个自定义的 CMake 模块,用于输出调试信息和配置检查。
```cmake
if(EXISTS "${PROJECT_SOURCE_DIR}/config.cmake")
include(config.cmake)
else()
message(FATAL_ERROR "Can't find cmake.config file")
endif()
```
如果存在 config.cmake 文件,则包含它,否则输出错误信息并停止构建。
```cmake
if(${CMAKE_CROSSCOMPILING})
set(LV_SIMULATOR_ON_PC OFF)
else()
set(LV_SIMULATOR_ON_PC ON)
endif()
```
根据交叉编译标志设置变量 LV_SIMULATOR_ON_PC。如果正在交叉编译,则设置为 OFF;否则设置为 ON。
```cmake
configure_file(
"${PROJECT_SOURCE_DIR}/base/base_sdk_version.h.in"
"${PROJECT_SOURCE_DIR}/base/base_sdk_version.h"
)
configure_file(
"${PROJECT_SOURCE_DIR}/base/base_config.h.in"
"${PROJECT_SOURCE_DIR}/base/base_config.h"
)
```
配置文件模板,用于生成编译时需要的头文件。
```cmake
set(CMAKE_INSTALL_BINDIR ${CMAKE_INSTALL_PREFIX}/bin)
set(CMAKE_INSTALL_SBINDIR ${CMAKE_INSTALL_PREFIX}/sbin)
set(CMAKE_INSTALL_LIBDIR ${CMAKE_INSTALL_PREFIX}/lib)
set(CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX}/include)
set(CMAKE_INSTALL_SYSCONFDIR ${CMAKE_INSTALL_PREFIX}/etc)
set(CMAKE_INSTALL_SHARESTATEDIR ${CMAKE_INSTALL_PREFIX}/com)
set(CMAKE_INSTALL_LOCALSTATEDIR ${CMAKE_INSTALL_PREFIX}/var)
set(CMAKE_INSTALL_INFODIR ${CMAKE_INSTALL_PREFIX}/info)
set(CMAKE_INSTALL_LOCALEDIR ${CMAKE_INSTALL_PREFIX}/locate)
set(CMAKE_INSTALL_MANDIR ${CMAKE_INSTALL_PREFIX}/man)
set(CMAKE_INSTALL_DOCDIR ${CMAKE_INSTALL_PREFIX}/doc)
set(CMAKE_INSTALL_RESDIR ${CMAKE_INSTALL_PREFIX})
```
设置安装目录的各个子目录。
```cmake
include(PrepareInstall)
```
包含一个自定义的 CMake 模块,用于准备安装目录。
```cmake
set(KIT_ROOT_DIR ${CMAKE_SOURCE_DIR})
```
设置变量 KIT_ROOT_DIR 的值为源代码目录。
```cmake
add_subdirectory("base")
add_subdirectory("lib")
add_subdirectory("drivers")
add_subdirectory("framework")
add_subdirectory("sysapp")
add_subdirectory("app")
```
添加子目录,用于构建项目的各个模块。每个子目录都包含了相应的 CMakeLists.txt 文件。
pro文件对应的cmake语句add_subdirectory是那句
在CMake中,add_subdirectory命令用于将子目录添加到构建中。对于一个包含.pro文件的项目,可以使用Qt的qmake工具来生成对应的CMakeLists.txt文件,然后在CMakeLists.txt中使用add_subdirectory命令来添加子目录。具体的语句会因为项目的不同而有所不同,需要根据具体情况来编写。
阅读全文