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

Overriding a default option(...) value in CMake from a parent CMakeLists.txt(从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值)

本文介绍了从父 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 中的默认选项(...)值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值

基础教程推荐