Hello and welcome back.
We're now going to discuss make files now what make files allow you to do they allow you to automate
the building of your project.
I know it's not going to code for you when I say automated building.
Think back to when we compiled application with GCSE main dot c dash o Main.
Now this is fine.
For one your program is only one file.
Imagine having 30 files and happened to manually do that for every single file and then link them all
together.
That's unacceptable and disastrous.
So make files allow you to specify a set of rules and then you can then run it through a program called
make which will then follow those rules to compile your entire project.
So let's begin.
So let's start by creating a new file and you're gonna call it make file has to be a capital M and no
extension just capital m a k e f i l e make file and now what I want you to do I want you to go all
Kola AND THEN I WANT YOU TO DO IN HERE New Line tab has to be a tab if you use spaces you'll make file
no work make file you can have spaces for the rules you need to use taps for the rules.
OK so we go GCSE main dot c dash so main as before.
And now although I you I want you to save that.
I want you then to open your turn animal and I want you to go Mingo to thirty two dash make just press
enter and I'll ask compiled your project and if we go back to our project that is compounded now.
So when you run this one command mean you think to make it'll build your whole project.
Now obviously we only have one one one command here being executed we want to make it so we can automate
the bill for the whole project so how do we do that.
Well just to demonstrate this let's create another file called it test of see and in here we're going
to have a function called my sum it takes two arguments and just call that should be this fine and all
it's going to do is return a plus b.
And now I want you to copy this because rather than create ahead of file here we're just going to define
mark this definition in Maine dot c so we don't have to create and you had a file so copy the prototype
here and just paste it here because remember when you include head of files that's all this is doing
is basically pasting this in the file that included it.
Right.
So we do that and now Maine dot see we'll be able to know that my sum is somewhere else in our program.
Let's just use it will resolve where it is later.
Remember remember that's what it says when we do this.
So now I want you to go into a equals my sum 50 20 so it adds those two together print f sent I fourth
slash and a.
OK.
Now if we compile that now with Mingo make you'll see that it has a linker error because we haven't
compiled tests of C right because we've said my sum is in another file or it or somewhere else in our
project and we haven't.
And we've told the compiler just remember that this is somewhere else.
Use it as if we have it and we'll resolve it later that's what we're saying.
But then we're not resolving it which is why we get this error.
Now let's now change our Make files so that it compiles test to see as well.
So I need you to create and make file variable which is pretty easy to do.
Just go up the top here and do object files equals top forward slash test Stop.
Oh that's all I wanted to do now in here.
I wanted to do after this colon I wanted to do percent and in these curlies object files.
So what this says is when we build our project we depend on all the object files in this variable.
And you define more variable more object files by doing things like this right.
And literally this when we do dollar and in these curlies it literally gets replaced with this.
That's what happens.
Okay so here.
Now we've now said to the make file okay we depend on these object files.
So we need to define those further down which we'll get to.
But firstly I want you to do in here.
After may not see dollar lease object files.
So again what this does this gets replaced with the value of object files which is test.
So if you remember this command this compiles may not see and links it against test auto resolving that
those symbols.
Now let's go further down here and we're going to create a new label called Test 0.
And we're gonna use our long and then we're gonna say test out See that's all I want to do press space.
I mean press enter and then do tab and do GCSE test out see Dash no test o dash C.
And while this does dash Siemens we're compiling this source file into an object file and not an excusable
because remember the object files get resolved later when we build the.
When we build the final project and we provide all the object files as we've just done here and it compiles
them all together.
So if we now go back to command prompt and we run Mingo 30 to make you can see it compiles tests all
see first because we've told it in in the old label that we need Tester o to continue.
So it compiles up first.
So.
So it comes down here and it compiles this and then the ole label is now satisfied.
So it runs our final command to compile main Delta C and link against tester o which was created in
the first command here that was executed.
So now we do mean it outputs 70 which is my some 50 20 which runs my sum function which returns a plus
b.
So that is make files.
And basically when you run make on its own what happens is it runs the first label it sees which in
our case is all.
Now you can also define other labels to do other things.
So for example here I think I can define a clean label which should delete those.
Those object files.
So if we now do DL dot for if we now do DL test auto and then we do.
Well one second Ed..
And then we do a n another.
And then we do another new line we do a tap here.
Main the XY press safe we now run make again.
I mean you mean good 30 to make
you can now see that a compulsive because I forgot to provide the clean.
So if you do space after that and you do clean you can see it deletes our program and object files so
you can basically if you provide a label after manga you make then it will run that label instead.
So if we did manga all is the equivalent to just doing manga 30 to make you understand.
So that is make files is very important.
You always have tabs here.
If you do a space here it won't work has to be tab.
Even if you do for four spaces to make up a tab it won't work has to be a tab guys.
If you don't do a tab this is what happens right.
I've done spaces.
Now if we can pop that again if we run that again missing separator.
Some of you probably seen that error to fix that.
Ensure that all your spaces are removed and replaced with tabs.
You do that.
And then it should work absolutely fine.
So that is make files and that's how you can automate the building of your project.
When you have like 20 C files you don't want to be entering this command for every single one of them.
So make files a load automate the whole thing.
They used in high scale projects all around the world especially in the open source community make is
a fantastic tool.