35

我正在尝试重用CMakeLists.txt我不想更改其来源的第三方项目(确切地说是expat )。我已将项目添加为顶层的子项目,使用add_subdirectory.

这可行,但现在我想option在顶层设置一些子项目的值CMakeLists.txt。我该怎么做呢?

4

4 回答 4

43

请参阅 具有良好答案的类似问题。

简而言之:

SET(SOME_EXPAT_OPTION OFF CACHE BOOL "Use some expat option")
于 2012-12-28T14:27:08.407 回答
39

如果子项目使用option(not set) 作为其配置设置,那么您可以option在添加子目录之前使用指定值:

option(LIB_OPTION1 "" OFF)
option(LIB_OPTION2 "" ON)
add_subdirectory(${CMAKE_SOURCE_DIRECTORY}/lib)
于 2014-12-08T14:31:11.550 回答
5

在调用之前,您可以使用所需的设置(ON 或 OFF)定义选项ADD_SUBDIRECTORY。这将优先OPTION于 expat 中的命令,CMakeLists.txt因为最后一个参数 toOPTION只是一个默认值(如果该设置已经存在,则忽略该值)。

于 2012-12-28T09:29:22.963 回答
-2

SET 命令具有“PARENT_SCOPE”选项:

If PARENT_SCOPE is present, the variable will be set in the scope above the current
scope. Each new directory or function creates a new scope. This command will set the 
value of a variable into the parent directory or calling function (whichever is 
applicable to the case at hand). PARENT_SCOPE cannot be combined with CACHE.

(见:http ://www.cmake.org/cmake/help/v2.8.10/cmake.html#command:set )

于 2012-12-28T09:35:40.520 回答