How do I know if find_package() succeeds in cmake?

我们两清 提交于 2021-01-27 18:33:55

问题


find_package(GTK)

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

Platform: windows XP


回答1:


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



来源:https://stackoverflow.com/questions/2711654/how-do-i-know-if-find-package-succeeds-in-cmake

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!