Home > Programmation > SFML avec cmake

SFML avec cmake

November 30th, 2009

L’objectif de ce billet est de montrer comment compiler une application C++ utilisant la bibliothèque SFML avec cmake.

Il faut bien entendu que cmake soit intallé, ainsi que la lib sfml.

Il n’y a malheureusement pas de module FindSFML livré avec cmake. J’en ai trouvé un il y a quelques temps sur le forum de la SFML qui semble ne plus être présent (régulièrement je reçois un mail me le demandant). J’ai testé ce module avec succès sous plusieurs distributions GNU/Linux, il ne fonctionne pas sous les non unix-like (merci de me faire part du module permettant d’utiliser la SFML de manière portable si vous en connaissez un).

Je créé un dossier pour contenir les modules supplémentaires :

  1. mkdir cmake_modules
  2. cd cmake_modules

Créez ensuite un fichier nommé FindSFML.cmake contenant

# Locate SFML library
# This module defines
# SFML_LIBRARY, the name of the library to link against
# SFML_FOUND, if false, do not try to link to SFML
# SFML_INCLUDE_DIR, where to find SFML headers
#
# Created by Nils Hasenbanck. Based on the FindSDL_*.cmake modules,
# created by Eric Wing, which were influenced by the FindSDL.cmake
# module, but with modifications to recognize OS X frameworks and
# additional Unix paths (FreeBSD, etc).

SET(SFML_COMPONENTS
System
Audio
Graphics
Network
Window
)

SET(SFML_INCLUDE_SEARCH_DIR
~/Library/Frameworks
/Library/Frameworks
/usr/local/include/SFML
/usr/include/SFML
/usr/local/include
/usr/include
/sw/include/SFML # Fink
/sw/include
/opt/local/include/SFML # DarwinPorts
/opt/local/include
/opt/csw/include/SFML # Blastwave
/opt/csw/include
/opt/include/SFML
/opt/include
)

SET(SFML_LIBRARY_SEARCH_DIR
~/Library/Frameworks
/Library/Frameworks
/usr/local
/usr
/sw
/opt/local
/opt/csw
/opt
)

FOREACH(COMPONENT ${SFML_COMPONENTS})
STRING(TOUPPER ${COMPONENT} UPPERCOMPONENT)
STRING(TOLOWER ${COMPONENT} LOWERCOMPONENT)
FIND_LIBRARY(SFML_${UPPERCOMPONENT}_LIBRARY
NAMES sfml-${LOWERCOMPONENT}
HINTS
$ENV{SFMLDIR}
PATH_SUFFIXES lib64 lib
PATHS ${SFML_LIBRARY_SEARCH_DIR}
)
FIND_PATH(SFML_${UPPERCOMPONENT}_INCLUDE_DIR ${COMPONENT}.hpp
HINTS
$ENV{SFMLDIR}
PATH_SUFFIXES include
PATHS ${SFML_INCLUDE_SEARCH_DIR}
)
IF(SFML_${UPPERCOMPONENT}_INCLUDE_DIR AND SFML_${UPPERCOMPONENT}_LIBRARY)
LIST(APPEND SFML_LIBRARY ${SFML_${UPPERCOMPONENT}_LIBRARY})
LIST(APPEND SFML_INCLUDE_DIR ${SFML_${UPPERCOMPONENT}_INCLUDE_DIR})
LIST(REMOVE_DUPLICATES SFML_LIBRARY)
LIST(REMOVE_DUPLICATES SFML_INCLUDE_DIR)
ENDIF(SFML_${UPPERCOMPONENT}_INCLUDE_DIR AND SFML_${UPPERCOMPONENT}_LIBRARY)
ENDFOREACH(COMPONENT)

SET(SFML_FOUND "NO")
IF(SFML_SYSTEM_LIBRARY AND SFML_INCLUDE_DIR)
SET(SFML_FOUND "YES")
ENDIF(SFML_SYSTEM_LIBRARY AND SFML_INCLUDE_DIR)

INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SFML DEFAULT_MSG SFML_LIBRARY SFML_INCLUDE_DIR)

Ensuite, pour compiler un projet avec la SFML, il faut utiliser un CMakeList du genre

project(test)
cmake_minimum_required(VERSION 2.6)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules")
set(CMAKE_CXX_FLAGS "-Wall -W -Werror -ansi -pedantic -g")
find_package(SFML REQUIRED)
SET(SOURCES
test.cpp
)

SET(EXECUTABLE_NAME
test
)

add_executable (
${EXECUTABLE_NAME} ${SOURCES}
)
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARY})

Et ensuite, la compilation habituelle avec cmake (je conseille de compiler dans un dossier séparé), personnellement je fais
mkdir build && cd build
cmake ..
make

Site officiel de Boost
Cmake

Programmation ,

  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.