cmake find_package脚本只能在Linux上失败

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cmake find_package脚本只能在Linux上失败相关的知识,希望对你有一定的参考价值。

我为Stanford Network Analysis Platform(SNAP)编写了一个包查找模块,它在macOS上按预期工作但在Linux上失败,由于某种原因,尽管显然找不到相关的路径/文件,但它报告没有找到Snap。

FindSnap.cmake

include(FindPackageHandleStandardArgs)
set(Snap_ROOT_DIR $ENV{WORKSPACE_ROOT}/opt/Snap)
message(STATUS "Snap_ROOT_DIR: " ${Snap_ROOT_DIR})

find_path(Snap_CORE
        NAMES "Snap.h"
        PATH_SUFFIXES snap-core
        HINTS ${Snap_ROOT_DIR}
        DOC "The Snap include directory")
message(STATUS "Snap core: " ${Snap_CORE})

find_path(Snap_GLIB_CORE
        NAMES "base.h"
        PATH_SUFFIXES glib-core
        HINTS ${Snap_ROOT_DIR}
        DOC "The Snap GLib include directory")
message(STATUS "Glib core: " ${Snap_GLIB_CORE})

find_library(Snap_LIBRARY
        NAMES libsnap.a
        HINTS ${Snap_ROOT_DIR}/snap-core
        DOC "The Snap library")
message(STATUS "Snap Library: " ${Snap_LIBRARY})

find_package_handle_standard_args(Snap_FOUND DEFAULT_MSG
        Snap_CORE
        Snap_GLIB_CORE
        Snap_LIBRARY)

if (Snap_FOUND)
    set(Snap_LIBRARIES ${Snap_LIBRARY})
    set(Snap_INCLUDE_DIRS ${Snap_CORE} ${Snap_GLIB_CORE})
    set(Snap_DEFINITIONS)
    message(STATUS "Snap Found: " ${Snap_INCLUDE_DIRS})
else()
    message(FATAL_ERROR "Package Snap not found")
endif (Snap_FOUND)

mark_as_advanced(Snap_ROOT_DIR Snap_INCLUDE_DIR Snap_LIBRARY)

在macOS上,Snap安装在/opt/Snap中,这个脚本从CMakeLists.txtfind_package(Snap REQUIRED)调用。在macOS上,这非常有效,但是在Ubuntu上,脚本报告找不到Snap,即使路径Snap_CORESnap_GLIB_CORESnap_LIBRARY似乎已找到。在Mac上我使用cmake版本3.10.2,在Linux上我已经尝试了2.8和3.9版本,但我仍然有同样的错误。

由于我无法在Linux机器上修改/opt,我在其他地方安装了Snap并修改了第二行的Snap根目录:

set(Snap_ROOT_DIR $ENV{WORKSPACE_ROOT}/elsewhere/Snap)

Linux机器上给出的错误:

-- Snap_ROOT_DIR: /afs/cs.stanford.edu/u/jdeaton/repos/snap
-- Snap core: /afs/cs.stanford.edu/u/jdeaton/repos/snap/snap-core
-- Glib core: /afs/cs.stanford.edu/u/jdeaton/repos/snap/glib-core
-- Snap Library: /afs/cs.stanford.edu/u/jdeaton/repos/snap/snap-core/libsnap.a
-- Found Snap_FOUND: /afs/cs.stanford.edu/u/jdeaton/repos/snap/snap-core
CMake Error at cmake/FindSnap.cmake:43 (message):
  Package Snap not found
Call Stack (most recent call first):
  CMakeLists.txt:6 (find_package)

看起来这应该工作正常,但不知何故它不起作用,只在Linux上失败。

谢谢!

答案

find_package_handle_standard_args的第一个论点应该是Snap而不是Snap_FOUND

 find_package_handle_standard_args(Snap DEFAULT_MSG
    Snap_CORE
    Snap_GLIB_CORE
    Snap_LIBRARY)

以上是关于cmake find_package脚本只能在Linux上失败的主要内容,如果未能解决你的问题,请参考以下文章

用于 Win32 平台的 CMake find_package(PythonInterp 3.7 REQUIRED)

cmake:Windows编译支持HTTPS的curl库及find_package查找CURL时需要注意的问题

CMake中find_package的使用

find_package()的查找*.cmake的顺序

CMake系列专题--find_package

Cmake命令之find_package介绍