windows 我怎么知道 find_package() 在 cmake 中是否成功?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2711654/
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
How do I know if find_package() succeeds in cmake?
提问by Gtker
find_package(GTK)
How can I make it output something so that I can know whether it finds something or not?
我怎样才能让它输出一些东西,以便我知道它是否找到了一些东西?
Platform: windows XP
平台:Windows XP
回答by Michael Aaron Safyan
You can use the messagecommand as in:
您可以使用message命令,如下所示:
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:
或者,如果您希望它在未找到 GTK 的情况下中止:
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:
请注意,如果您执行后者,那么您可以简单地将“REQUIRED”标志与 FIND_PACKAGE 一起使用,因为指定“REQUIRED”标志可确保如果找不到它,它将失败并显示错误:
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_PACKAGEfrom 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).
如果未找到 GTK,上面的命令将导致 CMake 中止并打印错误消息。您可能还对CMake 手册中的FIND_PACKAGE文档感兴趣。另外,应该注意 FIND_PACKAGE(XYZ) 实际上调用了 CMake 模块 FindXYZ,因此每个具有相应 FIND_PACKAGE 的包都有自己的 CMake 模块来实现查找操作......由于 CMake 仍然有点新,其中一些查找模块是未正确实施...根据您在下面的评论,似乎 FindGTK 未正确实施(因为如果它不存在,则使用 REQUIRED 标志应该会导致它因致命错误而中止,但不会在您的情况下似乎这样做)。