DEV Community

Callum
Callum

Posted on

Create SFML project with CMake

Depending on linux or windows some steps will vary but this tutorial will contain both
Install dependencies
Debian

$ sudo apt-get install libsfml-dev cmake g++
Enter fullscreen mode Exit fullscreen mode

Arch

$ sudo pacman -S sfml cmake gcc
Enter fullscreen mode Exit fullscreen mode

Windows
Download sfml
Download cmake
unzip latest stable to the route of your project folder

Create CMakeLists.txt
Current project folder:

Linux

BoilerPlate
  L CMakeLists.txt
Enter fullscreen mode Exit fullscreen mode

Windows

BoilerPlate
  L CMakeLists.txt
  L SFML
    L ...
Enter fullscreen mode Exit fullscreen mode

Open CMakeLists.txt and enter the following

cmake_minimum_required(VERSION 3.16)

project(BoilerPlate VERSION 1.0.0.0)

# Windows specific config
IF (WIN32)
    # Include local sfml cmake config
    set(SFML_DIR "${CMAKE_CURRENT_SOURCE_DIR}/SFML-2.5.1/lib/cmake/SFML")
    # Link sfml statically
    set(SFML_STATIC_LIBRARIES TRUE)
ENDIF()

# Find SFML shared libraries
find_package(SFML 2.5.1 COMPONENTS graphics audio REQUIRED)

FILE(
  GLOB
  SOURCES
  "main.cpp"
)

# Compile executable
add_executable(BoilerPlate ${SOURCES})

# Link executable to required SFML modules
target_link_libraries(BoilerPlate sfml-graphics sfml-audio)
Enter fullscreen mode Exit fullscreen mode

Create a main.cpp which we will create a base application that will open a window

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "BoilerPlate");

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.display();
    }
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Build and run

Linux

$ mkdir build
$ cd build
$ cmake ..
$ make
$ ./BoilerPlate
Enter fullscreen mode Exit fullscreen mode

Windows

$ mkdir build
$ cd build
$ cmake ..
Enter fullscreen mode Exit fullscreen mode

Open build folder,
Open BoilderPlate.sln
Right click on solution -> properties and select current selection
Build and run

You should have a window open

Top comments (0)