我怎么知道 find_package() 在 cmake 中是否成功? [英] How do I know if find_package() succeeds in cmake?

查看:68
本文介绍了我怎么知道 find_package() 在 cmake 中是否成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

find_package(GTK)

如何让它输出一些东西,以便我知道它是否找到了一些东西?

平台:windows XP

解决方案

您可以使用 message 命令如下:

<前>FIND_PACKAGE(GTK)如果(${GTK_FOUND})消息(状态找到 GTK.")其他(${GTK_FOUND})消息(状态无法找到 GTK.")ENDIF (${GTK_FOUND})

或者,如果您希望它在未找到 GTK 的情况下中止:

<前>FIND_PACKAGE(GTK)如果(${GTK_FOUND})消息(状态找到 GTK.")其他(${GTK_FOUND})MESSAGE(FATAL_ERROR "无法定位 GTK.")ENDIF (${GTK_FOUND})

请注意,如果您选择后者,那么您可以简单地将REQUIRED"标志与 FIND_PACKAGE 一起使用,因为指定REQUIRED"标志可确保它在未找到时失败并显示错误:

<前>FIND_PACKAGE(需要 GTK)

如果没有找到 GTK,上面的命令将导致 CMake 中止并打印错误消息.您可能还对 FIND_PACKAGE 来自 CMake 手册.另外,应该注意 FIND_PACKAGE(XYZ) 实际上调用了 CMake 模块 FindXYZ,因此每个具有相应 FIND_PACKAGE 的包都有自己的 CMake 模块来实现查找操作......由于 CMake 仍然有点新,其中一些查找模块是未正确实施...根据您在下面的评论,似乎 FindGTK 未正确实施(因为如果它不存在,则使用 REQUIRED 标志应该会导致它因致命错误而中止,但不会在你的情况下似乎这样做).

find_package(GTK)

How can I make it output something so that I can know whether it finds something or not?

Platform: windows XP

解决方案

You can use the message command as in:

FIND_PACKAGE(GTK)
IF (${GTK_FOUND})
   MESSAGE(STATUS "Found GTK.")
ELSE (${GTK_FOUND})
   MESSAGE(STATUS "Could not locate GTK.")
ENDIF (${GTK_FOUND})

Or, if you want it to abort if GTK isn't found:

FIND_PACKAGE(GTK)
IF (${GTK_FOUND})
   MESSAGE(STATUS "Found GTK.")
ELSE (${GTK_FOUND})
   MESSAGE(FATAL_ERROR "Could not locate GTK.")
ENDIF (${GTK_FOUND})

Note that if you do the latter, then you can simply use the "REQUIRED" flag with FIND_PACKAGE, as specifying the "REQUIRED" flag ensures that it will fail with an error if it isn't found:

FIND_PACKAGE(GTK REQUIRED)

The command above will cause CMake to abort and print an error message if GTK is not found. You may also be interested in the documentation for FIND_PACKAGE from the CMake Manual. Also, one should note that FIND_PACKAGE(XYZ) actually invokes the CMake module FindXYZ, and so each package with a corresponding FIND_PACKAGE has its own CMake module implementing the find operation... since CMake is stilll somewhat new, some of those find modules are not correctly implemented... based on your comments below, it would seem that FindGTK has not been implemented correctly (since if it isn't present, the use of the REQUIRED flag should cause it to abort with a fatal error, but does not seem to do so in your case).

这篇关于我怎么知道 find_package() 在 cmake 中是否成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆