无法 link sndfile 库到 cmake 项目 (MacOS)

Cannot link sndfile library to cmake project (MacOS)

目前我正在尝试为我的 uni 项目生成一些频谱图。我正在尝试构建一个静态库,所有魔法都可以在其中发挥作用,只需从 main() 函数中调用它即可。

这是我的 cmake 文件:

set(CMAKE_CXX_STANDARD 17)
project(demo)


find_package(SndFile REQUIRED)

add_subdirectory(spectogram)
add_executable(demo main.cpp)
target_link_libraries (demo LINK_PUBLIC Spectrogram)
target_link_libraries(demo PRIVATE SndFile::sndfile)

我已经通过自制程序安装了 libsndfile,但是 find_package() 拒绝定位该库并抛出此错误:

CMake Error at CMakeLists.txt:6 (find_package):
  By not providing "FindSndFile.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "SndFile", but
  CMake did not find one.

  Could not find a package configuration file provided by "SndFile" with any
  of the following names:

    SndFileConfig.cmake
    sndfile-config.cmake

  Add the installation prefix of "SndFile" to CMAKE_PREFIX_PATH or set
  "SndFile_DIR" to a directory containing one of the above files.  If
  "SndFile" provides a separate development package or SDK, be sure it has
  been installed.


-- Configuring incomplete, errors occurred!

我做了一些研究,发现 libsndfile 没有像其他库一样的任何 .cmake 配置,我可以 link 轻松地(如 OpenCV 或 spdlog)。我真的很感激任何解决这个恐怖的帮助。

在 Tsyvarev 的帮助下,我找到了解决方案。我使用了 pkg-config 模块和一个在网上找到的自定义 cmake file。我将包括我的最终 cmake,以防其他人需要它:

cmake_minimum_required(VERSION 3.17)
set(CMAKE_CXX_STANDARD 17)
project(demo)


# - Try to find libsndfile
# Once done, this will define
#
#  LIBSNDFILE_FOUND - system has libsndfile
#  LIBSNDFILE_INCLUDE_DIRS - the libsndfile include directories
#  LIBSNDFILE_LIBRARIES - link these to use libsndfile

# Use pkg-config to get hints about paths
find_package(PkgConfig QUIET)
if(PKG_CONFIG_FOUND)
    pkg_check_modules(LIBSNDFILE_PKGCONF sndfile)
endif(PKG_CONFIG_FOUND)

# Include dir
find_path(LIBSNDFILE_INCLUDE_DIR
        NAMES sndfile.h
        PATHS ${LIBSNDFILE_PKGCONF_INCLUDE_DIRS}
        )

# Library
find_library(LIBSNDFILE_LIBRARY
        NAMES sndfile libsndfile-1
        PATHS ${LIBSNDFILE_PKGCONF_LIBRARY_DIRS}
        )

find_package(PackageHandleStandardArgs)
find_package_handle_standard_args(LibSndFile  DEFAULT_MSG  LIBSNDFILE_LIBRARY LIBSNDFILE_INCLUDE_DIR)

if(LIBSNDFILE_FOUND)
    set(LIBSNDFILE_LIBRARIES ${LIBSNDFILE_LIBRARY})
    set(LIBSNDFILE_INCLUDE_DIRS ${LIBSNDFILE_INCLUDE_DIR})
endif(LIBSNDFILE_FOUND)

mark_as_advanced(LIBSNDFILE_LIBRARY LIBSNDFILE_LIBRARIES LIBSNDFILE_INCLUDE_DIR LIBSNDFILE_INCLUDE_DIRS)

include(FindPkgConfig)
pkg_search_module(SndFile REQUIRED sndfile)

include_directories(${LIBSNDFILE_INCLUDE_DIRS})

add_subdirectory(spectogram)
add_executable(demo main.cpp)

message(STATUS "sndfile include dirs path: ${LIBSNDFILE_INCLUDE_DIRS}")
message(STATUS "sndfile libs path: ${LIBSNDFILE_LIBRARIES}")

target_link_libraries (demo LINK_PUBLIC Spectrogram)
target_link_libraries(demo PRIVATE ${LIBSNDFILE_LIBRARIES})