How to build cmake ExternalProject while configurating main one?(如何在配置主要项目的同时构建 cmake ExternalProject?)
问题描述
当他们的安装目标搞砸时,引用 ExternalProjects 可能会很痛苦.因此,在为给定项目生成主项目文件之前,可能需要构建和安装 ExternalProjects 一次.是否可以使用 CMake 以及如何实现?
It can be a pain to refrence ExternalProjects when their install targets are messed up. So one may want to build and install ExternalProjects once before generating main project files for given project. Is it possible with CMake and how to do it?
推荐答案
您可以在 execute_process
中使用 cmake
调用来配置和构建包含 ExternalProject 的 CMake 项目:
You may use cmake
call within execute_process
for configure and build CMake project, which contains ExternalProject:
other_project/CMakeLists.txt:
project(other_project)
include(ExternalProject)
ExternalProject_Add(<project_name> <options...>)
CMakeLists.txt:
# Configure external project
execute_process(
COMMAND ${CMAKE_COMMAND} ${CMAKE_SOURCE_DIR}/other_project
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/other_project
)
# Build external project
execute_process(
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/other_project
)
这样的other_project 将在目录${CMAKE_BINARY_DIR}/other_project
中配置和构建.如果您没有在 ExternalProject_Add
调用中禁用安装,那么它会在构建 other_project 时执行.
Such a way other_project will be configured and built in directory ${CMAKE_BINARY_DIR}/other_project
. If you do not disable installation in ExternalProject_Add
call, then it will performed when building other_project.
通常,您希望从主项目中的变量推导出 ExternalProject 的一些选项,例如 SOURCE_DIR
、BINARY_DIR
、INSTALL_DIR
.您有两种方法可以实现:
Normally, you want some options to ExternalProject, like SOURCE_DIR
, BINARY_DIR
, INSTALL_DIR
, to be deduced from variables in the main project. You have two ways for achive that:
使用
configure_file
为 other_project 创建 CMakeLists.txt,从主项目调用(在execute_process
之前)命令).
Create CMakeLists.txt for other_project with
configure_file
, called from main project (beforeexecute_process
command).
将变量从主项目作为 -D
参数传递给 ${CMAKE_COMMAND}
.
Pass variables from main project as -D
parameters to ${CMAKE_COMMAND}
.
对顺序COMMANDS
进行分离 execute_process
调用很重要.否则,如果将单个 execute_process
与多个 COMMANDS
一起使用,这些命令将只是管道化";(同时执行,但第一个命令的输出被视为第二个命令的输入).
Having separated execute_process
calls for sequential COMMANDS
is important. Otherwise, if use single execute_process
with several COMMANDS
, these commands will be just "piped" (executed concurrently but with output of the first command being treated as input for the second).
这篇关于如何在配置主要项目的同时构建 cmake ExternalProject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在配置主要项目的同时构建 cmake ExternalProject?
基础教程推荐
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01