如何在配置主要项目的同时构建 cmake ExternalProject?

2023-08-28C/C++开发问题
12

本文介绍了如何在配置主要项目的同时构建 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_DIRBINARY_DIRINSTALL_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:

  1. 使用 configure_fileother_project 创建 CMakeLists.txt,从主项目调用(在 execute_process 之前)命令).

  1. Create CMakeLists.txt for other_project with configure_file, called from main project (before execute_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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6