如何检测include(SomeModule)是否有效?

时间:2018-06-30 05:23:29

标签: cmake

我使用的库可能会或可能不会导出 SomeModule.cmake 。如果存在,我想使用它来提供更好的功能,否则,我想使用一个简单的解决方法。

但是,如果include(SomeModule)失败,则CMake立即失败,并显示以下消息:

CMake Error at CMakeLists.txt:42 (include):
  include could not find load file:

    SomeModule

如何在不进行手动干预的情况下检测include(SomeModule)是否可以正常工作?


我在想像这样的东西:

# this function doesn't exist:
detect_include_exists(SomeModule)

if(SomeModule_FILE_EXISTS)
  include(SomeModule)

  # call the functions inside of SomeModule
else()
  # workaround code
endif()

1 个答案:

答案 0 :(得分:1)

命令include支持 OPTIONAL 参数,用于忽略缺少的文件。将其与RESULT_VARIABLE结合使用,您可以检查include()是否实际包含了文件:

include(SomeModule OPTIONAL RESULT_VARIABLE SomeModule_Found)
if(NOT SomeModule_Found)
     # Include file is absent. Need some workaround.
endif()