Things I don’t keep in mind for long, so I write them down.
A Gentle Introduction to Makefiles
To make things easier, we can put the compilation commands into a script. That script is called a Makefile. This post is a quick note on writing a Makefile with a simple example.
Writing a Makefile
Suppose we have four files: main.cpp, hello.cpp, square.cpp, and utils.h. Their contents are as follows:
Now let’s look at how and why it is written this way.
The simplest approach is to turn the compilation command into a script. For our files, that command is g++ main.cpp hello.cpp square.cpp -o main, which creates an executable named main.
The Makefile would look like this:
1 2
main: main.cpp hello.cpp square.cpp g++ main.cpp hello.cpp square.cpp -o main