|
1 | 1 | # CMake SFML Project Template
|
2 | 2 |
|
3 |
| -This repository template, should allow for a fast and hassle-free kick start of your next SFML project using CMake. |
| 3 | +This repository template should allow for a fast and hassle-free kick start of your next SFML project using CMake. |
4 | 4 | Thanks to [GitHub's nature of templates](https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-repository-from-a-template), you can fork this repository without inheriting its Git history.
|
5 | 5 |
|
6 | 6 | The template starts out very basic, but might receive additional features over time:
|
7 | 7 |
|
8 |
| -- Basic CMake script to build your project and link SFML |
| 8 | +- Basic CMake script to build your project and link SFML on any operating system |
9 | 9 | - Basic [GitHub Actions](https://github.com/features/actions) script for all major platforms
|
10 | 10 |
|
11 | 11 | ## How to Use
|
12 | 12 |
|
13 | 13 | 1. Follow the above instructions about how to use GitHub's project template feature to create your own project.
|
14 | 14 | 1. Open [CMakeLists.txt](CMakeLists.txt). Rename the project and the executable to whatever name you want. The project and executable names don't have to match.
|
15 |
| -1. Install SFML 2.5.1. |
| 15 | +1. Change the source files listed in [`add_executable`](CMakeLists.txt#L10) to match the source files your project requires. |
| 16 | +1. Configure and build your project. Most popular IDEs support CMake projects with very little effort on your part. |
| 17 | + - [VS Code](https://code.visualstudio.com) via the [CMake extension](https://code.visualstudio.com/docs/cpp/cmake-linux) |
| 18 | + - [Visual Studio](https://docs.microsoft.com/en-us/cpp/build/cmake-projects-in-visual-studio?view=msvc-170) |
| 19 | + - [CLion](https://www.jetbrains.com/clion/features/cmake-support.html) |
| 20 | + - [Qt Creator](https://doc.qt.io/qtcreator/creator-project-cmake.html) |
16 | 21 |
|
17 |
| - On Ubuntu Linux: |
| 22 | + Using CMake from the command line is straightforward as well. |
| 23 | + |
| 24 | + For a single-configuration generator (typically the case on Linux and macOS): |
18 | 25 | ```
|
19 |
| - sudo apt install libsfml-dev |
| 26 | + cmake -S . -B build -DCMAKE_BUILD_TYPE=Release |
| 27 | + cmake --build build |
20 | 28 | ```
|
21 |
| - On macOS |
| 29 | +
|
| 30 | + For a multi-configuration generator (typically the case on Windows): |
22 | 31 | ```
|
23 |
| - brew install sfml |
| 32 | + cmake -S . -B build |
| 33 | + cmake --build build --config Release |
24 | 34 | ```
|
25 |
| -1. Change the source files listed in [`add_executable`](CMakeLists.txt#L6) to match the source files your project requires. |
26 |
| -1. Configure and build your project. |
| 35 | +1. Enjoy! |
27 | 36 |
|
28 |
| - On Linux or macOS, run these commands from the root directory of your project. |
29 |
| - ```cmake |
30 |
| - cmake -B build |
31 |
| - cmake --build build |
32 |
| - ``` |
| 37 | +## Upgrading SFML |
33 | 38 |
|
34 |
| - Alternatively, use [VS Code](https://code.visualstudio.com) with its [CMake extension](https://code.visualstudio.com/docs/cpp/cmake-linux) on any OS. |
35 |
| -1. Enjoy! |
| 39 | +SFML is found via CMake's [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html) module. |
| 40 | +FetchContent automatically downloads SFML from GitHub and builds it alongside your own code. |
| 41 | +Beyond the convenience of not having to install SFML yourself, this ensures ABI compatability and simplifies things like specifying static versus shared libraries. |
| 42 | +
|
| 43 | +Modifying what version of SFML you want is as easy as changing the [`GIT_TAG`](CMakeLists.txt#L7) argument. |
| 44 | +Currently it uses the latest in-development version of SFML 2 via the `2.6.x` tag. |
| 45 | +If you're feeling adventurous and want to give SFML 3 a try, use the `master` tag. |
| 46 | +Beware, this requires changing your code to suit the modified API! |
| 47 | +The nice folks in the [SFML community](https://github.com/SFML/SFML#community) can help you with that transition and the bugs you may encounter along the way. |
| 48 | +
|
| 49 | +## But I want to... |
| 50 | +
|
| 51 | +Modify CMake options by adding them as configuration parameters (with a `-D` flag) or by modifying the contents of CMakeCache.txt and rebuilding. |
| 52 | +
|
| 53 | +### Use Static Libraries |
| 54 | +
|
| 55 | +By default SFML builds shared libraries and this default is inherited by your project. |
| 56 | +CMake's [`BUILD_SHARED_LIBS`](https://cmake.org/cmake/help/latest/variable/BUILD_SHARED_LIBS.html) option lets you pick static or shared libraries for the entire project. |
| 57 | +
|
| 58 | +### Change Compilers |
| 59 | +
|
| 60 | +See the variety of [`CMAKE_<LANG>_COMPILER`](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER.html) options. |
| 61 | +In particular you'll want to modify `CMAKE_CXX_COMPILER` to point to the C++ compiler you wish to use. |
| 62 | +
|
| 63 | +### Change Compiler Optimizations |
| 64 | +
|
| 65 | +CMake abstracts away specific optimizer flags through the [`CMAKE_BUILD_TYPE`](https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html) option. |
| 66 | +By default this project recommends `Release` builds which enable optimizations. |
| 67 | +Other build types include `Debug` builds which enable debug symbols but disable optimizations. |
| 68 | +If you're using a multi-configuration generator (as is often the case on Windows), you can modify the [`CMAKE_CONFIGURATION_TYPES`](https://cmake.org/cmake/help/latest/variable/CMAKE_CONFIGURATION_TYPES.html#variable:CMAKE_CONFIGURATION_TYPES) option. |
| 69 | +
|
| 70 | +### Change Generators |
| 71 | +
|
| 72 | +While CMake will attempt to pick a suitable default generator, some systems offer a number of generators to choose from. |
| 73 | +Ubuntu, for example, offers Makefiles and Ninja as two potential options. |
| 74 | +For a list of generators, click [here](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html). |
| 75 | +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. |
| 76 | +You can't simply modify an entry in the CMakeCache.txt file unlike the above options. |
| 77 | +Then you may rebuild your project with this new generator. |
36 | 78 |
|
37 | 79 | ## More Reading
|
38 | 80 |
|
|
0 commit comments