簡體   English   中英

無法將 sndfile 庫鏈接到 cmake 項目 (MacOS)

[英]Cannot link sndfile library to cmake project (MacOS)

目前我正在嘗試為我的 uni 項目生成一些頻譜圖。 我正在嘗試構建一個 static 庫,所有魔法都可以在其中發揮作用,只需從 main() function 調用它。

這是我的 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)

我已經通過 homebrew 安裝了 libsndfile ,但是 find_package() 拒絕找到該 lib 並拋出此錯誤:

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 配置,就像其他庫一樣,我可以輕松鏈接(如 OpenCV 或 spdlog)。 我真的很感激任何幫助來解決這個恐怖。

在 Tsyvarev 的幫助下,我找到了解決方案。 我使用了 pkg-config 模塊和自定義cmake 文件,我在 web 上找到了該文件。 我將包括我的最終 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})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM