Cmake“设置”在“find_path”之后不适用于同一变量被称为

发布于 2024-10-17 12:23:07 字数 537 浏览 1 评论 0原文

在 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)

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

多谢!

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:

# 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?

Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从﹋此江山别 2024-10-24 12:23:07

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

该行:

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

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

如果你想让它可见,你必须将它的值强制放入同名的缓存变量中,也许像这样:

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

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

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

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

所以...鉴于您的代码是正确的,那么一定有其他原因导致您认为出了问题。我很好奇那可能是什么......也许是您的下一个 Stack Overflow 问题。

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

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 not visible in the cmake-gui or ccmake cache editing programs.

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.

To see that it really does take effect, simply add this code at the end of 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文