With SFML 2.5.0 we have modernized our CMake build system largely thanks for Ceylo! :)
In the last weeks however we've noticed a considered amount of people not realizing the changes and thus wondering what to do with their "broken" build system. This thread should help clarify the required changes to utilize the new SFMLConfig.cmake file.
FindSFML.cmake has been removed
You can look for it as long as you want, it doesn't exist anymore, regardless of guide XYZ said, if you use SFML 2.5.0 there's no FindSFML.cmake anymore.
This also means, you no longer have to make sure that FindSFML.cmake can be found in CMAKE_MODULE_PATH. As such if you have some code like SET(CMAKE_MODULE_PATH ...) you can remove that from your CMake file.
SFML_ROOT is no more
Windows users (and maybe others) have gotten used to setting SFML_ROOT in order to tell the FindSFML.cmake where to look for SFML. This isn't true anymore and instead the error message with suggestion CMake throws at you, can finally be implemented.
Could not find a package configuration file provided by "SFML" (requested version 2.5) with any of the following names:
SFMLConfig.cmake
sfml-config.cmake
Add the installation prefix of "SFML" to CMAKE_PREFIX_PATH or set "SFML_DIR" to a directory containing one of the above files. If "SFML" provides a separate development package or SDK, be sure it has been installed.
Meaning, if the SFMLConfig.cmake file can't be automatically located, you can provide the directory containing said file by setting SFML_DIR.
${SFML_LIBRARIES}, ${SFML_DEPENDENCIES} and ${SFML_INCLUDE_DIR} don't exist anymore
This is now mostly automated. The include directory no longer needs to be manually set, so this step can be completely removed. As for the libraries, all you need to specify is the dependencies on the highest level in a dependency tree. Meaning, if you want sfml-graphics, you only need to search for the graphics module and link the sfml-graphics library, CMake will take care of resolving the additionally dependencies like sfml-window or sfml-system. Even for static libraries, you don't need to specify the dependencies as CMake will do it for your.
Example CMake script
Update: Thrasher and I have put together a CMake-SFML project template, which also features quite a few useful links in the ReadMe: https://github.com/eXpl0it3r/cmake-sfml-project
Here's an example script that links sfml-graphics and sfml-audio and their dependencies.
cmake_minimum_required(VERSION 3.1)
project(SFMLTest)
## If you want to link SFML statically
# set(SFML_STATIC_LIBRARIES TRUE)
## In most cases better set in the CMake cache
# set(SFML_DIR "<sfml root prefix>/lib/cmake/SFML")
find_package(SFML 2.5 COMPONENTS graphics audio REQUIRED)
add_executable(SFMLTest main.cpp)
target_link_libraries(SFMLTest sfml-graphics sfml-audio)
Documentation
A more detailed usage documentation can be found directly inside the SFMLConfig.cmake file (https://github.com/SFML/SFML/blob/master/cmake/SFMLConfig.cmake.in).
While you're here, you might as well try to get your project updated to modern CMake. This article (https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right/) might be useful for you. ;)
IDE: CLion
System: Windows 10
SFML: 3.5.1 downloaded from https://www.sfml-dev.org/files/SFML-2.5.1-windows-gcc-7.3.0-mingw-64-bit.zip
I've unzipped the package to <project>/library/frameworks/ and used the CMakeLists.txt below but got:
The following configuration files were considered but not accepted
C:/projects/sfml/./library/frameworks/SFML-2.5.1/lib/cmake/SFML/SFMLConfig.cmake, version: 2.5.1 (64bit)
cmake_minimum_required(VERSION 3.14)
project(sfml)
set(CMAKE_CXX_STANDARD 11)
# SFML
## If you want to link SFML statically
set(SFML_STATIC_LIBRARIES TRUE)
## In most cases better set in the CMake cache
set(SFML_DIR "./library/frameworks/SFML-2.5.1/lib/cmake/SFML")
find_package(SFML 2.5 COMPONENTS system window graphics network audio REQUIRED)
add_executable(sfml main.cpp)
target_link_libraries(sfml sfml-system sfml-window sfml-graphics sfml-network sfml-audio)