This repository template should allow for a fast and hassle-free kick start of your next SFML project using CMake. Thanks to GitHub's nature of templates, you can fork this repository without inheriting its Git history.
The template starts out very basic, but might receive additional features over time:
- Basic CMake script to build your project and link SFML on any operating system
- Basic GitHub Actions script for all major platforms
-
Install Git and CMake. Use your system's package manager if available.
-
Follow GitHub's instructions for how to use their project template feature to create your own project. If you don't want to use GitHub, see the section below.
-
Clone your new GitHub repo and open the repo in your text editor of choice.
-
Open CMakeLists.txt. Rename the project and the target name of the executable to whatever name you want. Make sure to change all occurrences.
-
If you want to add or remove any .cpp files, change the source files listed in the
add_executable
call in CMakeLists.txt to match the source files your project requires. If you plan on keeping the default main.cpp file then no changes are required. -
If your code uses the Audio or Network modules then add
sfml-audio
orsfml-network
to thetarget_link_libraries
call alongside the existingsfml-graphics
library that is being linked. -
If you use Linux, install SFML's dependencies using your system package manager. On Ubuntu and other Debian-based distributions you can use the following commands:
sudo apt update sudo apt install \ libxrandr-dev \ libxcursor-dev \ libudev-dev \ libfreetype-dev \ libflac-dev \ libvorbis-dev \ libgl1-mesa-dev \ libegl1-mesa-dev \ libfreetype-dev
-
Configure and build your project. Most popular IDEs support CMake projects with very little effort on your part.
Using CMake from the command line is straightforward as well. Be sure to run these commands in the root directory of the project you just created.
cmake -B build cmake --build build
-
Enjoy!
SFML is found via CMake's FetchContent module. FetchContent automatically downloads SFML from GitHub and builds it alongside your own code. Beyond the convenience of not having to install SFML yourself, this ensures ABI compatibility and simplifies things like specifying static versus shared libraries.
Modifying what version of SFML you want is as easy as changing the GIT_TAG
argument.
Currently it uses SFML 3 via the 3.0.0
tag.
Modify CMake options by adding them as configuration parameters (with a -D
flag) or by modifying the contents of CMakeCache.txt and rebuilding.
You can use this project without a GitHub account by downloading the contents of the repository as a ZIP archive and unpacking it locally. This approach also avoids using Git entirely if you would prefer to not do that.
See the variety of CMAKE_<LANG>_COMPILER
options.
In particular you'll want to modify CMAKE_CXX_COMPILER
to point to the C++ compiler you wish to use.
CMake abstracts away specific optimizer flags through the CMAKE_BUILD_TYPE
option.
By default this project recommends Release
builds which enable optimizations.
Other build types include Debug
builds which enable debug symbols but disable optimizations.
If you're using a multi-configuration generator (as is often the case on Windows), you can modify the CMAKE_CONFIGURATION_TYPES
option.
While CMake will attempt to pick a suitable default generator, some systems offer a number of generators to choose from.
Ubuntu, for example, offers Makefiles and Ninja as two potential options.
For a list of generators, click here.
To modify the generator you're using you must reconfigure your project providing a -G
flag with a value corresponding to the generator you want.
You can't simply modify an entry in the CMakeCache.txt file unlike the above options.
Then you may rebuild your project with this new generator.
Here are some useful resources if you want to learn more about CMake:
- Official CMake Tutorial
- How to Use CMake Without the Agonizing Pain - Part 1
- How to Use CMake Without the Agonizing Pain - Part 2
- Better CMake YouTube series by Jefferon Amstutz
The source code is dual licensed under Public Domain and MIT -- choose whichever you prefer.