如何在CMake sfml中链接FLAC?

问题描述 投票:0回答:1

我是cmake和sfml的新手,我想学习cpp游戏开发。 在我的 Linux 机器上,我收到此错误:

[1/1] Linking CXX executable SFMLApp
FAILED: SFMLApp 
: && /usr/bin/c++ -g  CMakeFiles/SFMLApp.dir/main.cpp.o -o SFMLApp -L/home/issa/CLionProjects/Graphics_demo/SFML_LIBRARY_DIR -Wl,-rpath,/home/issa/CLionProjects/Graphics_demo/SFML_LIBRARY_DIR:/home/issa/Cpp-libs/SFML-2.6.1/lib  /home/issa/Cpp-libs/SFML-2.6.1/lib/libsfml-graphics-d.so.2.6.1  /home/issa/Cpp-libs/SFML-2.6.1/lib/libsfml-window-d.so.2.6.1  /home/issa/Cpp-libs/SFML-2.6.1/lib/libsfml-audio-d.so.2.6.1  /home/issa/Cpp-libs/SFML-2.6.1/lib/libsfml-system-d.so.2.6.1 && :
/usr/bin/ld: warning: libFLAC.so.12, needed by /home/issa/Cpp-libs/SFML-2.6.1/lib/libsfml-audio-d.so.2.6.1, not found (try using -rpath or -rpath-link)
/usr/bin/ld: /home/issa/Cpp-libs/SFML-2.6.1/lib/libsfml-audio-d.so.2.6.1: undefined reference to `FLAC__stream_decoder_skip_single_frame'
/usr/bin/ld: /home/issa/Cpp-libs/SFML-2.6.1/lib/libsfml-audio-d.so.2.6.1: undefined reference to `FLAC__stream_decoder_process_until_end_of_metadata'
/usr/bin/ld: /home/issa/Cpp-libs/SFML-2.6.1/lib/libsfml-audio-d.so.2.6.1: undefined reference to `FLAC__stream_decoder_seek_absolute'
/usr/bin/ld: /home/issa/Cpp-libs/SFML-2.6.1/lib/libsfml-audio-d.so.2.6.1: undefined reference to `FLAC__stream_encoder_init_file'
/usr/bin/ld: /home/issa/Cpp-libs/SFML-2.6.1/lib/libsfml-audio-d.so.2.6.1: undefined reference to `FLAC__stream_encoder_finish'
/usr/bin/ld: /home/issa/Cpp-libs/SFML-2.6.1/lib/libsfml-audio-d.so.2.6.1: undefined reference to `FLAC__stream_decoder_finish'
/usr/bin/ld: /home/issa/Cpp-libs/SFML-2.6.1/lib/libsfml-audio-d.so.2.6.1: undefined reference to `FLAC__stream_encoder_process_interleaved'
/usr/bin/ld: /home/issa/Cpp-libs/SFML-2.6.1/lib/libsfml-audio-d.so.2.6.1: undefined reference to `FLAC__stream_encoder_set_channels'
/usr/bin/ld: /home/issa/Cpp-libs/SFML-2.6.1/lib/libsfml-audio-d.so.2.6.1: undefined reference to `FLAC__stream_encoder_new'
/usr/bin/ld: /home/issa/Cpp-libs/SFML-2.6.1/lib/libsfml-audio-d.so.2.6.1: undefined reference to `FLAC__stream_decoder_new'
/usr/bin/ld: /home/issa/Cpp-libs/SFML-2.6.1/lib/libsfml-audio-d.so.2.6.1: undefined reference to `FLAC__stream_decoder_delete'
/usr/bin/ld: /home/issa/Cpp-libs/SFML-2.6.1/lib/libsfml-audio-d.so.2.6.1: undefined reference to `FLAC__stream_encoder_set_sample_rate'
/usr/bin/ld: /home/issa/Cpp-libs/SFML-2.6.1/lib/libsfml-audio-d.so.2.6.1: undefined reference to `FLAC__stream_decoder_process_single'
/usr/bin/ld: /home/issa/Cpp-libs/SFML-2.6.1/lib/libsfml-audio-d.so.2.6.1: undefined reference to `FLAC__stream_decoder_get_state'
/usr/bin/ld: /home/issa/Cpp-libs/SFML-2.6.1/lib/libsfml-audio-d.so.2.6.1: undefined reference to `FLAC__stream_decoder_init_stream'
/usr/bin/ld: /home/issa/Cpp-libs/SFML-2.6.1/lib/libsfml-audio-d.so.2.6.1: undefined reference to `FLAC__stream_encoder_delete'
/usr/bin/ld: /home/issa/Cpp-libs/SFML-2.6.1/lib/libsfml-audio-d.so.2.6.1: undefined reference to `FLAC__stream_encoder_set_bits_per_sample'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

我安装了 libflac-dev:

 $ sudo find /* -name libFLAC.so.*
...
/usr/lib/x86_64-linux-gnu/libFLAC.so.8
/usr/lib/x86_64-linux-gnu/libFLAC.so.8.3.0
...

我应该做什么改变来链接 CMakeLists.txt 中的 libflac-dev:

cmake_minimum_required(VERSION 3.25)
project(Graphics_demo)

set(CMAKE_CXX_STANDARD 17)

add_executable(Graphics_demo main.cpp)

set(EXECUTABLE_NAME "SFMLApp")
set(SFML_INCLUDE_DIR "/home/issa/Cpp-libs/SFML-2.6.1/include")
set(SFML_LIBRARY_DIR "/home/issa/Cpp-libs/SFML-2.6.1/lib")
set(SFML_DIR "/home/issa/Cpp-libs/SFML-2.6.1/lib/cmake/SFML")

link_directories(SFML_LIBRARY_DIR)
include_directories(SFML_INCLUDE_DIR)

find_package(SFML 2.6.1 COMPONENTS system window graphics audio network)
if (SFML_FOUND)
    message(STATUS "SFML_INCLUDE_DIR: ${SFML_INCLUDE_DIR}")
    message(STATUS "SFML_LIBRARIES: ${SFML_LIBRARIES}")
    message(STATUS "SFML_VERSION: ${SFML_VERSION}")
endif ()

add_executable(SFMLApp main.cpp)
target_link_libraries(SFMLApp sfml-graphics sfml-window sfml-system sfml-audio)

为什么这个命令可以正常工作而 cmake 却不能?

 g++ main.cpp -o main -lsfml-system -lsfml-graphics -lsfml-audio -lsfml-window
c++ ubuntu cmake sfml
1个回答
0
投票

解决方案:

我终于得到了解决方案,通过从

这里
手动安装libflac12,之后我得到了
faild to load sound error
并且也解决了这里

说明:

使用

sudo apt install libsfml-dev
时安装的 sfml apt 版本是
2.5.1
libFLAC.so.8
配合良好,使用
g++
时使用此版本。
我在 CMakeLists.txt 中使用的 sfml 版本是
2.6.1
,它需要
libFLAC.so.12

© www.soinside.com 2019 - 2024. All rights reserved.