从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值

2023-08-27C/C++开发问题
22

本文介绍了从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我试图在我的源代码树中包含几个第三方库,并对其构建系统进行最少的更改,以便于升级.他们和我一样都使用 CMake,所以在我自己的 CMakeLists.txt 中,我可以将 add_subdirectory(extern/foo) 用于 libfoo.

I am trying to include several third-party libraries in my source tree with minimal changes to their build system for ease of upgrading. They all use CMake, as do I, so in my own CMakeLists.txt I can use add_subdirectory(extern/foo) for libfoo.

但是 foo CMakeLists.txt 编译测试工具、构建文档、我不需要的共享库,等等.libfoo 作者有先见之明通过选项控制这些 - option(FOO_BUILD_SHARED "Build libfoo shared library" ON) 例如 - 这意味着我可以通过 CMake 命令行设置它们.但我想默认关闭它并且可以通过命令行覆盖.

But the foo CMakeLists.txt compiles a test harness, builds documentation, a shared library which I don't need, and so on. The libfoo authors had the foresight to control these via options - option(FOO_BUILD_SHARED "Build libfoo shared library" ON) for example - which means I can set them via the CMake command line. But I would like to make that off by default and overridable via the command line.

我试过在 add_subdirectory(extern/foo) 之前做 set(FOO_BUILD_SHARED OFF).这具有在第二次和后续构建尝试期间不尝试构建共享库的效果,但在第一次尝试构建共享库时不会尝试,这是我真正需要加速的一次.

I have tried doing set(FOO_BUILD_SHARED OFF) before add_subdirectory(extern/foo). That has the effect of not trying to build the shared library during the second and subsequent build attempts, but not during the first one, which is the one I really need to speed up.

这是可能的,还是我需要为这些项目维护分叉的 CMakeLists.txt?

Is this possible, or do I need to maintain forked CMakeLists.txt for these projects?

推荐答案

尝试设置 CACHE 中的变量

Try setting the variable in the CACHE

SET(FOO_BUILD_SHARED OFF CACHE BOOL "Build libfoo shared library")

注意:您需要指定变量类型和描述,以便 CMake 知道如何在 GUI 中显示此条目.

Note: You need to specify the variable type and a description so CMake knows how to display this entry in the GUI.

这篇关于从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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