DEV Community

Cover image for How to write a good makefile for C++
David Mendoza (He/Him)
David Mendoza (He/Him)

Posted on • Updated on

Makefile C++ How to write a good makefile for C++

Hello guys, its me again today I'm going to show you how to write a good makefile for a c++ project that you might have.

What's a makefile

Just if you didn't knew I want to explain it, Basically a makefile its an easier way to compile our code, let me be clear It's not language exclusive you can use it on c++ just as you can use it in python, but most people use them on low level programming languages.

How is it going to help me

Lets say that you have a big project, maybe 10 or 20 classes and you main file, so what's the compile line going to look like

g++ -Wall -Wextra -std=c++17 -ggdb -Iinclude -Llib src/AssetManager.cpp src/Game.cpp src/InputManager.cpp src/SplashState.cpp src/StateMachine.cpp src/GameState.cpp src/main.cpp -o bin/main -lsfml-graphics -lsfml-audio -lsfml-window -lsfml-system -llua53 -ldl
Enter fullscreen mode Exit fullscreen mode

So this is the compile line for my Skeleton engine project.

GitHub logo mendoza / skeleton-engine

Small game engine or at least its skeleton

Skeleton Engine 💀 made with ❤️, and some 🍺

License Issues forks stars size Unicorn



Small game engine framework , made with just 4 dependencies.

  1. SFML Repo
  2. OpenEcs Repo
  3. Lua53 Repo
  4. Sol3 Repo

Which 3 of them are implemented on the source right away (OpenEcs, Sol3 and Lua)


Just for fun and learning


Be sure to have sfml.

sudo apt-get install libsfml-dev #debian based OS
Enter fullscreen mode Exit fullscreen mode
git clone
cd Skeleton-Engine
cmake .
Enter fullscreen mode Exit fullscreen mode

Bugs 🐛

This project is getting upgrades in my free time if there is a problem please create a bug report in the issues section.


But as you can see its pretty long...
So imagine writing that every time you need to compile (lets say that terminals don't store last commands hahaha)

What do I do then?

you create a makefile on your project's root, here is a example

touch makefile
Enter fullscreen mode Exit fullscreen mode

so now your directory looks like this

├── bin/
├── include/
├── lib/
├── makefile
└── src/
Enter fullscreen mode Exit fullscreen mode

I strongly recommend this "layout", you will place all .h or .hpp in your include directory, and your .cpp in your src directory, and precompiled libraries (.a, or .out) are going in your lib directory.

CXX       := g++
CXX_FLAGS := -Wall -Wextra -std=c++17 -ggdb

BIN     := bin
SRC     := src
INCLUDE := include
LIB     := lib


run: clean all
    @echo "🚀 Executing..."

$(BIN)/$(EXECUTABLE): $(SRC)/*.cpp
    @echo "🚧 Building..."
    $(CXX) $(CXX_FLAGS) -I$(INCLUDE) -L$(LIB) $^ -o $@ $(LIBRARIES)

    @echo "🧹 Clearing..."
    -rm $(BIN)/*
Enter fullscreen mode Exit fullscreen mode

What's all wizary your are seeing? well lets go step by step.

  • CXX Its the "program" you are using for compiling (g++ in my case)
  • CXX_FLAGS Its the flags for your compiling program
  • BIN this is where your binary file is going to be placed
  • SRC As I told you this is where all your .cpp are going to be
  • INCLUDE Of course this is all your .hpp or .h file are going to be
  • LIB As I said is where your precompiled files are going to be
  • LIBRARIES This are the libraries flags for g++
  • EXECUTABLE Its just the name of your binary file
  • all It the "recipe" your makefile will call if you just call make on your terminal
  • run It compiles your code and makes it run
  • $(BIN)/$(EXECUTABLE) this one just compiles the code
  • clean it cleans all your binary files.

If you need any help defining any recipe for your makefile let me know! I will be glad to help you out on anything.

Now that I have everything setup what do I do?

Its really simple, look at this,

to compile your code you call this in your terminal

Enter fullscreen mode Exit fullscreen mode

and to compile it and run it, you call this in your terminal

make run
Enter fullscreen mode Exit fullscreen mode

And that's all really, go ahead and try it!

if you got any problem leave me a comment and I will help you out

Top comments (3)

sarahferind profile image
Sarah Masri Ferind

awesome article thanks!

Whilst researching c++ makefiles I found another interesting example here:

daduam profile image
Joseph Ampadu

thanks for this

mendoza profile image
David Mendoza (He/Him)

Hope you make something great with it