29
Make Command: A Magical Way for Automating Stuff
In this mini-tutorial, you will learn how to write a
Makefile
for automating stuff from project builds to whatnot, which is executed by themake
command in Linux
Cause it's used in many many projects for building stuff cause when you have to compile thousands of files for a project and ensure that each file is older than the compiled file, then Makefile
saves the day!
Imagine you have a project which has tons of files dependent on one or another and they need to be compiled at once, compiling them manually is gonna take way too much time!
Also, if you are compiling a file that is already compiled from the latest version of the source file, then you would be compiling it again (what a waste of time and resources!).
You can write a script file for automating the whole process but for ensuring the dependencies are compiled properly, it's gonna get real complicated real soon.
make
uses a Makefile
for automating the whole process of building stuff from start to finish.
If dependencies are files, it will ensure they are updated and if they are already updated according to the source code, it won't update them (Saving time and resources!).
Also, you can individually build each dependency with make
if you want to.
target: dependency
action
target: target file or command that will be invoked when a user executes the
make
command with following Command-Line Argumentmake target
dependency (optional): they can be as many as you want (space separated) and they are a dependency for building that specific target
action: that's where you type the commands that need to be executed for building that target
If a user simply executes
make
without any option, the target namedall
will be executed.Before typing the command for building the target in action, ensure to add a
tab
instead of spaces or it will keep saying ":makefile:4: *** missing separator. Stop." or something like that.Dependencies can be files as well in the local directory.
Save the following code in a file and name it Makefile
all: one two
echo "Executed all"
one:
echo "Building one"
two:
echo "Building two (part 1)"
echo "Building two (part 2)"
I kept it simple so we can understand how make
works...
make
echo "Building one"
Building one
echo "Building two (Part 1)"
Building two (Part 1)
echo "Building two (Part 2)"
Building two (Part 2)
echo "Executed all"
Executed all
- It Builds dependencies first if don't exist
- It Builds the target when dependencies exist
It checks if dependency one and two exist, if they don't exist (or is outdated), They become a target and their dependencies are checked, if they don't have any, then the commands (actions) are executed for building them.
Then finally, the all target is built using those dependencies.
If for some reason, you wanna build the individual dependencies, simply specify the target while executing the make
command.
make one
echo "Building one"
Building one
If it had any dependency, it would have built before this.
make two
echo "Building two (Part 1)"
Building two (Part 1)
echo "Building two (Part 2)"
Building two (Part 2)
Now you know a little bit about how you can use the make
command in Linux for automating stuff from builds to what not.
If you find this article useful, then leave a like and support me by following me on Twitter.
Thanks for reading π