c++ubuntucmakesfml

How to link FLAC in CMake sfml?


I am new to cmake and sfml, and i want to learn cpp game development. on my linux machine i got this error:

[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.

i have libflac-dev installed:

 $ 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
...

What change should i do in to link libflac-dev in CMakeLists.txt:

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)

why this command work without an errors while the cmake one doesn't?

 g++ main.cpp -o main -lsfml-system -lsfml-graphics -lsfml-audio -lsfml-window

Solution

  • solution:

    I finally got the solution, by installing libflac12 manually from here, after that i got the faild to load sound error and also solved here.

    Explanation:

    The apt vertion of sfml which is installed when using sudo apt install libsfml-dev is 2.5.1 works fine with libFLAC.so.8, and this version is used with the g++ command since it's executed from the system terminal.
    The version of sfml which i am using in the CMakeLists.txt is 2.6.1 which requires libFLAC.so.12.