This is a beginner friendly cmake
for C
projects. while it's not by all means the "best" approach but it's clearly crafted together and commented for you to be able to adjust it to your needs.
cmake
any utility and re-usable files for your cmakes goes here.extern
folder is where you put your external dependencies, as an example I have usedclove-unit
library for unit testing.include
is where you put your exported header files.src
is where your.c
files must be placed.tests
are written usingclove-unit
framework which is a lightweight and single header library.
👉 NOTE: You can refer to here for more information about clove-unit
.
👉 NOTE: There is also .clang-format
available for you to use.
👉 NOTE: All the build artifacts will be placed in out
folder, all the build artifacts for tests will be placed in out_tests
folder.
By reading their source codes as they are fully commented you can understand pretty much everything you need.
But the main workflow of using this template is like this:
-
in main
CMakeLists.txt
:- Rename project name in line 12.
- Rename your desired executable file
main_exe_file
on lines 20, 25, 37 and 46. - Every time you add a new
.c
file you add it inEXECUTABLE TARGETS
section to one or more of your executable files. - If you want to add another Target aka. executable or library feel free to repeat what's on lines 20 to 23 also make sure to rename the variables to meet your needs.
- In case you want also to enable pre build testing meaning re-building and running tests every time you build certain target look at line 37.
-
Testing (
tests/CMakeLists.txt
):- You create a new
.c
file intests
folder, name it liketest_<whatever>.c
, it's for readability but you don't have to. - Follow
clove-unit
library guide and write your test. - use
add_clove_test
with the exact nametest_<whatever>
without.c
inCMakeLists.txt
oftests
directory to register your test. - 👉 NOTE: Make sure to read the guide in the
tests
folder cmake file.
- You create a new
-
Visual Studio Code Users:
- Make sure C/C++ and CMake Tools extensions are installed.
- If you're on Windows and you haven't installed your tools yet, make sure to check out here.
- Just open the folder like normal and let the magic happen.
-
CodeBlocks and Visual Studio 2022 Users:
- You need to create corresponding projects first.
- Open terminal in the folder (make sure you've deleted
build
folder if it already exists):- Enter
cmake -B build -G"CodeBlocks - Unix Makefiles"
if you need CodeBlocks project. - Enter
cmake -B build -G"Visual Studio 17 2022"
if you need Visual Studio project.
- Enter
- Now open up the
build
folder and you can find project or solution for your IDE.
-
KDevelop Users just open the folder and the IDE itself recognizes the
CMakeLists.txt
and will guide you through it. -
Adding Configurations to define macros:
You can use
define_macro_option(<target name> <macro name in your code> <default value ON/OFF>)
after your executables or libraries. this will add<target name>_ENABLE_<macro name you've provided>
cmake option.👉 Example: Checkout usage example and its corresponding effect in the main source.
-
Adding post build file copy:
You can use
define_post_built_copy(<target name> <relative destination> <files...>)
to enable copying of any files relative to executables or libraries.👉 NOTE: no
./
is needed in destination, pass""
if you want the files being copied to the root of the output directory.👉 Example: Checkout usage example.
Looking at the official cmake template of the raylib
I thought it might be good to add it to my template as well.
As it is mentioned in the line 7 you can remove that line when your project don't need raylib.
Enabling raylib
is as simple as just using enable_raylib_for
command like enable_raylib_for(game_example)
at line 31.
👉 NOTE: To include and manage your resources, use resources folder with a subfolder per game project.
Check out this place. it's a good book and I highly recommend you to read it if you want to get more into cmake.
BSD 3-Clause License
Copyright (c) 2024, Navid Dezashibi navid@dezashibi.com
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
-
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
-
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
-
Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.