If you'd prefer to watch the youtube video on this topic, please click here.
What Is A Make File?
A "make" file is a simple bash script that is traditionally used to compile/run our code files. This means make files usually live in the "src" directory. (Hey, what's that logo at the top left of this screen say?)
Use make files to build commands to handle different situations you need to compile or clean up your project. See the next section for example situations.
How To Make Our Own Make File
Imagine that we're creating a C++ project. For now, let's just start with some simple Hello World code in a file named main.cpp.
To compile this, we'd use the following command in a terminal:
This compiles our main.cpp file into an executable named "run". To use run, we'd then do the following:
And our terminal will output "Hello World, from srcmake". So with that, we have our C++ file, we know the command to compile it, and we know how to run it.
To create a make file, we just need to write the following into a file named "makefile":
To use our make file, all we need to do is type "make" into the terminal.
And like that, the terminal will execute the commands in sequence. We no longer have to memorize the actual commands. (Keep reading to learn why we need the "all:" part.)
But why would we even use a make file?
Well...imagine that now we have multiple files. Let's move the hello world part of our program into a separate file, to demonstrate this.
In a file named helper.cpp, we have the following code:
helper.cpp contains a simple function that says Hello World. Well, now we need a header file to match this .cpp file, so let's create a file called helper.h
And finally, our main.cpp file will now include this header, and make use of the SayHi() function. Our main.cpp is now:
Our project just got a lot more complex. Now we have 3 files in this folder (not including the make file, or the run file). Not only that, now to compile this, we need the do the following:
Notice that we had to include the helper.cpp file to be compiled. Well...once your project starts having more than ten files, you really don't want to type all the file names out every single time we compile our code. Let's just tell our make file the file name!
In the terminal, we still have the same simple word to command and run our program!
And so, make files make our compiling (and running, and more) a lot easier.
Is that all make files are good for? No, they can do much more.
Special Commands for Make Files
So far, we've only used the word "make" in the terminal and it does one thing. But actually, that's because we only specified the "all" part of make. Actually, we can make our make file as complex and have as much complexity as we want.
Let's change our make file to the following:
We now have the following commands available to us (to type in the terminal):
Our "all" parameter specifies what the default "make" command does. In our case, it gives a message to the user.
Our "make build" will compile our C++ program.
Our "make doit" command will execute the run file (if it exists). Notice that we can't name the parameter "run" but instead must name it something else, like "doit". That's because the make file would assume ./run was referring to the make parameter 'run' (if it existed). Confusing? Yeah.
Our "make clean" command removes the executable file.
These are just some examples of what you can do with make files. The way you want to use a make file is up to you and your project, but using one is definitely preferred over memorizing the commands for compiling.
If you'd like to watch the youtube video to see how these commands work, please watch the video embedded below:
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
1. Me! Where do you think I came up with the name "srcmake" from?