 Okay, so let's get started. In this directory, I have three source files. I have hello.cpp, hello.h, and main.cpp. If I compile these files using g++, as you can see, we get hello world back. And let me just show you what these files actually do. Whoops. Okay, so what these files do is essentially we have hello.cpp that just prints out hello world. We have hello.h that's just declaring the hello function. In the main cpp, we're just executing the hello function. That's pretty simple. So to actually build these files using makefile, what we're going to do is we are going to open makefile. So make sure you get a name called makefile, don't call it anything else. And here what we're going to do is we are going to just define a new rule. We're going to define a new rule which is going to target main.out. So we're going to say we want to build the file main.out. And to build this file main.out, what do we need? Well, we need two files as you saw previously. We need hello.cpp and we need main.cpp. Okay, so in makefile, this is called the target and these are called the prerequisites. So to build this target, to make this file. As the name implies, to make a file main.out, we need hello.cpp and we need main.cpp. Okay, so on this line, this is the recipe. This is how you actually build this file. So on this line, we're going to say g++, main.and then hello.cpp. And then we're going to output main.out, right quit, save that. Let's see if we have this, now we don't and then we write make. We just type make and then hit enter. And as you can see, this magically ran this command for us. And let's check this file and we see that we got main.out back. Let's execute main.out and as you expect to get hello world. Okay, that's awesome. Okay, so why is this useful? Why do you want to use make? Well, let me just demonstrate a very useful aspect of make. So let's say I write make again. Well, it gives me back this message that says, main.out is up to date. What does that mean? Well, what does it mean it's up to date? Let's just look at the listing here and let's check this out. Okay, so main.out was built at 10.15. And let's see main.cpp, the last time I updated main.cpp was at 8.31. And the last time I updated hello.cpp was at 10.15. So main.cpp and hello.cpp are actually older than main.out. So logically I already built this. So I already built this and these two files actually older. So there's no point in rebuilding this main.out because this main.out is actually newer than main.cpp and hello.cpp. But if we update main.cpp or hello.cpp, it's gonna force make to rebuild main.out because at that point main.out will be older than this file or this file. So let's actually do that. So the easiest way to update a file would be to just touch it, right? So we just do touch and then we're gonna touch main.cpp and last.lh. And as you can see now, we see that main.out is built at 10.25 and main.cpp was built at, well not built, but last updated at 10.32. Okay, so if we run make again, as you can see it builds the file again. So this is why you wanna use make. In make, every time you update a file, it's going to selectively go in and compile only the files that need to be recompiled. So if there are files that do not need to be recompiled, it will not recompile them, which actually saves you a lot in terms of compilation speed. So it may not look that impressive now with just two files. If you have like 25 files or 30 files, it's gonna actually save you a lot of time if you only need to compile the files that have been updated since the last time you ran make. Okay, now that we understand it, let's break out our main file into several rules. So let's open make. Okay, so here we have hello.cpp and main.cpp as the prerequisites for this main.out file. And the problem here is that both the main.cpp and hello.cpp need to be compiled every single time. So every single time we change either main.cpp or hello.cpp, both files need to be recompiled. And that makes this make file inefficient. So what I'm gonna do is I'm gonna define a couple more rules here, right? So I'm gonna make a rule for hello.o and this hello.o is gonna have a prerequisite that's gonna be called hello.cpp. And here what we're gonna do is gonna say g++-c and then we are going to compile hello.cpp and I'm gonna output that as hello.o. Great, okay, next up. What we're gonna do is we're gonna say main.o. So this is gonna be the main.o.object file and we're gonna say this has a prerequisite of main.cpp. Then we're gonna compile this, oops. I'm gonna compile this, main.cpp and then dash o, main.o. Okay, great. So now that we have these two rules, we have a rule for making hello.o and we have a rule for making main.o. Okay, so instead of having the prerequisites be main.cpp and hello.cpp, we're gonna say that prerequisites now are gonna be hello.o and main.o. So basically what this is gonna do is it's going to look at these two prerequisites and say, okay, do we have hello.o and do we have main.o? And if we do, or if we don't, it's gonna go down here and it's gonna say, okay. So this is the target for hello.o. How do we build this? It's gonna say this prerequisite is hello.cpp. In that case, it's gonna compile, it's gonna check for that file. Then what it's gonna do is it's going to say hello.cpp, we're gonna compile this and I'm gonna output this as hello.o. And also make sure you go down here and you change this to main.o and hello.o. Okay, so basically what we're doing now is we are compiling it separately. So we're compiling hello.o and main.o. And if either hello.cpp or main.cpp change, we only compile main.o or hello.o based on which source file changed. Okay, great. So let's write quit and we're gonna clear this. I'm gonna make, and as you can see here, we compiled hello.o, we compiled main.cpp, or should I say we compiled main.o and then we compiled this as well. And let's execute this, oops. Okay, and let's execute main.o and we get hello world back. Okay, great. So let's change one of the files. So we can say vim main.cpp. Okay, so we're gonna change this and we are just going to run hello twice. And then if we type make, we didn't recompile hello.o, we only recompiled main.o. What this file is doing is it's checking the prerequisites against the target. So it goes and checks hello.o and then checks which is older. Is hello.o older than hello.cpp? If hello.cpp is younger than hello.o, it's gonna recompile hello.o. If main.o is older than main.cpp, then it's gonna recompile main.o. So essentially what this is doing is it's just checking which is older and then if it's older, it's gonna run this command on here, right? So basically that's the gist of it. That's basically all you need to know this is like 90% of make file. If you understand this, you basically understand make file. If you like this video, please like comment and subscribe. Okay, so with this make file, we have three rules and basically this works pretty well. This is fine. This is for most of your problems, this is gonna work just fine for you. You can just like keep extending it as much as you want. But let's say we want to like modify it, right? Let's say we want to change the compiler from G plus plus to Clang or to GCC or something like that. How do we do that? Well, we could optionally just go in here and say like GCC, GCC or Clang and then Clang. Now here's the problem with this. As we type out, as we get more and more files, as we get like, let's say like in the future we have like a hundred files here. We're gonna have to modify this in a hundred places. And instead of doing that, we can optionally just set a variable. We can go up here. We can make a variable called, let's say we're gonna call this like a CC or something, right? And then we're gonna assign it to G plus plus. And then down here, instead of typing, you know, G plus plus, we can just do something like this. CC, close this, CC. So basically down here what we're doing is we're using variable instead of just using like a hard-coded compiler. So instead of using a compiler such as like G plus plus, I can just go up here and I can say, okay, instead of using G plus plus, what we're gonna do is we are gonna use a Clang or we're gonna use GCC. I'm just gonna keep this as G plus plus for now and let's recompile this or let's remake this to see if this works. Okay, I think I need to update everything here. Okay, so as you can see, this command still ran, we're still using G plus plus. Now, another thing you can do is you can also specify flags. So for example, so for example, we can say, we can say flags and then what kind of flags do we want? Let's say we want, let's say we want optimization level three and down here we can just do something like, and as you can see, we're optimizing everything with the dash or three. Okay, so another thing you can do is you can set the name of the output to just be automatically the name of the target. So if your target and the output name here are the same, you can essentially just use a built-in variable into make, which is just dollar sign at sign. And then you can do the same down here. So this is really useful for if you want to change the name of the target, you can just do this without having to change it into places at the same time. So let's make this, and as you can see, again, this got substituted with the name of the target. Okay, and the last thing that we can do is we can change, let's say we have a hello.o and main.o. If we change the name here from hello.o and main.o to something else, down here we are probably gonna change it too, right? So what we can do is we can just, instead of typing out all of the prerequisites down here that we're using, we can just use a variable, which is gonna look like this. And it's gonna say dollar sign, then carrot symbol, I believe. And basically this is just gonna take all of the prerequisites and then paste them here. It's gonna substitute this symbol with all of the prerequisites, which is actually pretty useful. And down here, same, we don't want to hard code this. I'm gonna say dollar sign, then carrot, then down here again, dollar sign, and then carrot. And here we are gonna compile this, or we're gonna make this again, type make. And as you can see, basically all of our prerequisites got substituted. Okay, I mean, that's pretty much if you need to know more about make, I recommend you go to the official website for make and check it out.