windows 调用“find_path”后,Cmake“set”对同一变量不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5006286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:11:36  来源:igfitidea点击:

Cmake "set" doesn't work on same variable after "find_path" was called

windowsmacoscmake

提问by Chris

Within a Cmake module I am trying to locate different paths. Under some circumstances I would like to "set" a variable after I initially called "find_path" with the same variable:

在 Cmake 模块中,我试图找到不同的路径。在某些情况下,我想在我最初使用相同的变量调用“find_path”后“设置”一个变量:

# general search for this include dir
find_path(LIBRARY_INCLUDE_DIR
  NAMES LibraryName/LibraryHeader.h
)

# specific option enabled by user
if(USE_OTHER_LIB)
find_path(OTHER_LIB_ROOT_DIR
  NAMES OtherLib/OtherLib.h
)
set(LIBRARY_INCLUDE_DIR ${OTHER_LIB_ROOT_DIR}/database/include)
endif(USE_OTHER_LIB)

This approach did work fine under Windows XP (CMake 2.8.1). However, it did not work under Mac OS 10.6 (CMake 2.8.3). Does somebody know if there is a difference between the mac / windows version and how to resolve this?

这种方法在 Windows XP (CMake 2.8.1) 下运行良好。但是,它在 Mac OS 10.6 (CMake 2.8.3) 下不起作用。有人知道mac / windows版本之间是否存在差异以及如何解决这个问题吗?

Thanks a lot!

非常感谢!

回答by DLRdave

This is a common misconception about "set" and CMake cache variables.

这是对“set”和 CMake 缓存变量的常见误解。

The line:

线路:

set(LIBRARY_INCLUDE_DIR ${OTHER_LIB_ROOT_DIR}/database/include)

sets a local override value for LIBRARY_INCLUDE_DIR that takes effect for the remainder of the processing of the CMakeLists file, but it has no effect on the cache variable of the same name. Therefore, it is notvisible in the cmake-gui or ccmake cache editing programs.

为 LIBRARY_INCLUDE_DIR 设置一个本地覆盖值,该值对 CMakeLists 文件的其余处理生效,但对同名的缓存变量没有影响。因此,它在 cmake-gui 或 ccmake 缓存编辑程序中是可见的。

If you wanted to make it visible, you would have to force its value into the cache variable of the same name, perhaps like this:

如果要使其可见,则必须将其值强制放入同名的缓存变量中,可能如下所示:

set(LIBRARY_INCLUDE_DIR ${OTHER_LIB_ROOT_DIR}/database/include)
set(LIBRARY_INCLUDE_DIR ${LIBRARY_INCLUDE_DIR} CACHE FILEPATH "" FORCE)

This is generally frowned upon, however, because then when an end user adjusts the value in the cmake-gui program, your code will overwrite the users choice with the FORCE-d value. So... I would recommend just using the line you've been using: resist the FORCE.

然而,这通常是不受欢迎的,因为当最终用户调整 cmake-gui 程序中的值时,您的代码将用 FORCE-d 值覆盖用户选择。所以......我建议只使用你一直在使用的路线:抵抗力量。

To see that it really does take effect, simply add this code at the end of CMakeLists.txt:

要查看它是否真的生效,只需在 CMakeLists.txt 的末尾添加以下代码:

message(STATUS "LIBRARY_INCLUDE_DIR='${LIBRARY_INCLUDE_DIR}'")

So... given that your code is correct, there must be something else going on that causes you to think that something's wrong. I'm curious about what that might be... perhaps your next Stack Overflow question.

所以...考虑到您的代码是正确的,肯定还有其他原因导致您认为某些事情是错误的。我很好奇那可能是什么......也许你的下一个 Stack Overflow 问题。