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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more