CMake构建带Vorbis(OGG)支持的静态libsndfile失败求助
问题:CMake配置中libsndfile无法启用Vorbis支持
我尝试在项目中通过CMake引入libogg、libvorbis和libsndfile以播放OGG文件,但构建后libsndfile始终没有Vorbis支持。以下是我的CMake配置代码:
# Fetch libogg FetchContent_Declare( libogg GIT_REPOSITORY https://github.com/xiph/ogg.git GIT_TAG v1.3.5 ) FetchContent_MakeAvailable(libogg) # Fetch libvorbis FetchContent_Declare( libvorbis GIT_REPOSITORY https://github.com/xiph/vorbis.git GIT_TAG v1.3.7 ) SET(OGG_INCLUDE_DIR ${libogg_BUILD_DIR}/include) SET(OGG_LIBRARY ${libogg_BUILD_DIR}/libogg.a) FetchContent_MakeAvailable(libvorbis) # Fetch libsndfile FetchContent_Declare( libsndfile GIT_REPOSITORY https://github.com/libsndfile/libsndfile.git GIT_TAG 1.2.0 # Replace with the version you need ) SET(BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libraries" FORCE) SET(Vorbis_Vorbis_LIBRARY ${libvorbis_BINARY_DIR}/lib/libvorbis.a) SET(Vorbis_Vorbis_INCLUDE_DIR ${libvorbis_SOURCE_DIR}/include) FetchContent_MakeAvailable(libsndfile)
运行cmake ..后的输出显示libsndfile未找到Vorbis:
-- CMake Deprecation Warning at build/_deps/libogg-src/CMakeLists.txt:1 (cmake_minimum_required): Compatibility with CMake < 3.5 will be removed from a future version of CMake. Update the VERSION argument <min> value or use a ...<max> suffix to tell CMake that the project does not need compatibility with older versions. -- Configuring libogg 1.3.5 CMake Deprecation Warning at build/_deps/libvorbis-src/CMakeLists.txt:1 (cmake_minimum_required): Compatibility with CMake < 3.5 will be removed from a future version of CMake. Update the VERSION argument <min> value or use a ...<max> suffix to tell CMake that the project does not need compatibility with older versions. -- Configuring vorbis 1.3.7 -- Found Ogg: /libogg.a CMake Deprecation Warning at build/_deps/libsndfile-src/CMakeLists.txt:1 (cmake_minimum_required): Compatibility with CMake < 3.5 will be removed from a future version of CMake. Update the VERSION argument <min> value or use a ...<max> suffix to tell CMake that the project does not need compatibility with older versions. CMake Deprecation Warning at build/_deps/libsndfile-src/CMakeLists.txt:14 (cmake_policy): The OLD behavior for policy CMP0091 will be removed from a future version of CMake. The cmake-policies(7) manual explains that the OLD behaviors of all policies are deprecated and that a policy should be set to OLD only under specific short-term circumstances. Projects should be ported to the NEW behavior and not rely on setting a policy to OLD. -- Could NOT find ALSA (missing: ALSA_LIBRARY ALSA_INCLUDE_DIR) -- Found Ogg: /libogg.a (Required is at least version "1.3") -- Could NOT find FLAC (missing: FLAC_LIBRARY FLAC_INCLUDE_DIR) -- Could NOT find Opus (missing: OPUS_LIBRARY OPUS_INCLUDE_DIR) -- Could NOT find mp3lame (missing: MP3LAME_LIBRARY MP3LAME_INCLUDE_DIR) -- Could NOT find Mpg123 (missing: MPG123_LIBRARY MPG123_INCLUDE_DIR) (Required is at least version "1.25.10") -- Could NOT find Speex (missing: SPEEX_LIBRARY SPEEX_INCLUDE_DIR) -- The following features have been enabled: * INSTALL_PKGCONFIG_MODULE, generate and install pkg-config module * INSTALL_MANPAGES, install man pages for programs -- The following OPTIONAL packages have been found: * LibUSB * Nettle (required version >= 3.0) * GnuTLS * OpenSSL * BZip2 * LibLZMA (required version >= 5.2) * zstd (required version >= 1.3.6) * SQLite3, light weight SQL database engine., <www.sqlite.org/> Enables regtest -- The following REQUIRED packages have been found: * ZLIB * Ogg, library for manipulating ogg bitstreams, <www.xiph.org/ogg/> Required to enable Vorbis, Speex, and Opus support -- The following features have been disabled: * BUILD_SHARED_LIBS, build shared libraries * ENABLE_EXTERNAL_LIBS, enable FLAC, Vorbis, and Opus codecs * ENABLE_MPEG, enable MPEG audio (including mp3) codecs * ENABLE_EXPERIMENTAL, enable experimental code * BUILD_TESTING, build tests * BUILD_REGTEST, build regtest * ENABLE_CPACK, enable CPack support * ENABLE_CPU_CLIP, Enable tricky cpu specific clipper * ENABLE_BOW_DOCS, enable black-on-white html docs * ENABLE_PACKAGE_CONFIG, generate and install package config file -- The following OPTIONAL packages have not been found: * webp * MbedTLS (required version >= 1.0) * PulseAudio * OSS * AudioIO * JACK * OpenSL * PortAudio * MySOFA * SndFile * ALSA * Mpg123 (required version >= 1.25.10) * Speex, an audio codec tuned for speech, <www.speex.org/> Enables experemental Speex support -- The following RECOMMENDED packages have not been found: * Vorbis, open source lossy audio codec, <www.vorbis.com/> Enables Vorbis support * FLAC, Free Lossless Audio Codec Library, <www.xiph.org/flac/> Enables FLAC support * Opus, Standardized open source low-latency fullband codec, <www.opus-codec.org/> Enables experimental Opus support * mp3lame, High quality MPEG Audio Layer III (MP3) encoder, <https://lame.sourceforge.io/> Enables MPEG layer III (MP3) writing support
请问配置哪里出错了?
解决方案
你的配置存在三个关键问题:
未启用外部编解码器开关
libsndfile默认关闭ENABLE_EXTERNAL_LIBS选项,这个开关是启用Vorbis、FLAC等第三方编解码器的前提,必须显式开启。Vorbis变量名称错误
libsndfile查找Vorbis时使用的变量是VORBIS_LIBRARY、VORBISFILE_LIBRARY和VORBIS_INCLUDE_DIR,而非你设置的Vorbis_Vorbis_LIBRARY格式。其中libvorbisfile是Vorbis文件处理必需的库,不能遗漏。库路径引用可能不准确
需要确保引用的libvorbis静态库路径正确,通常构建后libvorbis.a和libvorbisfile.a会生成在libvorbis_BINARY_DIR的lib子目录下。
修正后的CMake配置代码如下:
# Fetch libogg FetchContent_Declare( libogg GIT_REPOSITORY https://github.com/xiph/ogg.git GIT_TAG v1.3.5 ) FetchContent_MakeAvailable(libogg) # Fetch libvorbis FetchContent_Declare( libvorbis GIT_REPOSITORY https://github.com/xiph/vorbis.git GIT_TAG v1.3.7 ) # 传递libogg的路径给libvorbis SET(OGG_INCLUDE_DIR ${libogg_SOURCE_DIR}/include) SET(OGG_LIBRARY ${libogg_BINARY_DIR}/libogg.a) FetchContent_MakeAvailable(libvorbis) # Fetch libsndfile FetchContent_Declare( libsndfile GIT_REPOSITORY https://github.com/libsndfile/libsndfile.git GIT_TAG 1.2.0 # Replace with the version you need ) # 强制静态库构建 SET(BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libraries" FORCE) # 启用外部编解码器支持 SET(ENABLE_EXTERNAL_LIBS ON CACHE BOOL "Enable FLAC, Vorbis, Opus codecs" FORCE) # 设置Vorbis相关路径 SET(VORBIS_LIBRARY ${libvorbis_BINARY_DIR}/lib/libvorbis.a) SET(VORBISFILE_LIBRARY ${libvorbis_BINARY_DIR}/lib/libvorbisfile.a) SET(VORBIS_INCLUDE_DIR ${libvorbis_SOURCE_DIR}/include) # 传递libogg路径给libsndfile SET(OGG_LIBRARY ${libogg_BINARY_DIR}/libogg.a) SET(OGG_INCLUDE_DIR ${libogg_SOURCE_DIR}/include) FetchContent_MakeAvailable(libsndfile)
重新运行CMake后,你应该会看到libsndfile检测到Vorbis支持的日志,并且ENABLE_EXTERNAL_LIBS会被标记为启用状态。
内容的提问来源于stack exchange,提问作者Justin808
相关产品推荐
相关产品推荐

