上层CMakeLists.txt设置的${CMAKE_INSTALL_PREFIX}怎么传到子目录里
时间: 2023-12-15 14:04:43 浏览: 190
可以使用CMake中的`add_subdirectory`命令来添加子目录,同时可以通过设置子目录的变量来传递上层CMakeLists.txt设置的`${CMAKE_INSTALL_PREFIX}`。
例如,上层CMakeLists.txt中定义了`${CMAKE_INSTALL_PREFIX}`变量,然后在添加子目录时,可以使用以下方式将该变量传递到子目录中:
```
add_subdirectory(child_dir)
set_target_properties(child_target PROPERTIES INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})
```
其中,`child_dir`为子目录名称,`child_target`为子目录中的目标名称,可以在子目录的CMakeLists.txt中使用`${INSTALL_PREFIX}`变量来引用`${CMAKE_INSTALL_PREFIX}`的值。
相关问题
上层CMakeLists.txt设置的${CMAKE_INSTALL_PREFIX}变量怎么传到子目录里使用,你刚才说的是错的
非常抱歉,我之前的回答有误。正确的方法是,在子目录的CMakeLists.txt中,可以使用`CMAKE_INSTALL_PREFIX`变量来引用`${CMAKE_INSTALL_PREFIX}`变量的值。
例如,在上层CMakeLists.txt中定义`${CMAKE_INSTALL_PREFIX}`变量,可以在子目录的CMakeLists.txt中使用以下方式来引用该变量:
```
message(STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}")
```
这里使用了`message`命令来打印`${CMAKE_INSTALL_PREFIX}`变量的值,可以在CMake的输出中查看该值。子目录中的所有目标都可以使用`${CMAKE_INSTALL_PREFIX}`变量来引用`${CMAKE_INSTALL_PREFIX}`的值。
同时,也可以在`add_subdirectory`命令中使用`CMAKE_INSTALL_PREFIX`选项来指定`${CMAKE_INSTALL_PREFIX}`变量的值。例如:
```
add_subdirectory(child_dir INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})
```
这样可以将`${CMAKE_INSTALL_PREFIX}`变量的值传递给子目录,并在子目录的CMakeCache.txt文件中设置`CMAKE_INSTALL_PREFIX`变量的值。子目录中的所有目标都可以使用`${CMAKE_INSTALL_PREFIX}`变量来引用`${CMAKE_INSTALL_PREFIX}`的值。
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 文件。
阅读全文