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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more