c++cmakelibsndfile

Cannot link sndfile library to cmake project (MacOS)


Currently I'm trying to make some spectogram generation for my uni project. I'm trying to build a static library where all the magic will work and just call it from the main() function.

This is my cmake file:

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)

I have installed libsndfile via homebrew, but find_package() refuses to locate the lib and throws this error:

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!

I`ve made some research and found out that libsndfile does not have any .cmake configs inside like other libs, that I can link easily (like OpenCV or spdlog). I would really appreciate any help to solve this horror.


Solution

  • With help of Tsyvarev, I figured out the solution. I used the pkg-config module and a custom cmake file, I found on the web. I will include my final cmake in case someone else will need it:

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