In this lecture we're gonna be covering header files and object files.
So we've briefly discussed how it including this had a file here gives you access to print functionality
and the ability to read data in from the keyboard.
Well we need to be able to create our own Head of files so that we can share parts of our program with
other parts of our program.
Or if we wanted to create a library which we'll get on to later on we would need a head file to be given
to the people who want to use our library so.
So that there so that the compiler knows how to talk with our library.
These are all great uses for header files.
So essentially she had files they store they store definitions and function prototypes.
They can store structures as well.
Basically they can store anything that we can store in this file.
But the main purpose of head of files is to export functionality to someone who will be calling functions
or accessing our library in some way.
So yes in this lecture we won't be creating a library we'll be creating object files and header files.
So let's get to it then.
What I want you to do I want you to create a new file called test to see.
Okay cool.
And inside here we're going to have a void function called my test.
It takes voids.
No arguments.
And we're going to include the CIO header files so we can output to.
So the terminal again has CDI ODI H and I just wanted to print f.
Hello.
How are you and your line.
Okay cool.
So the question now how do we access my test from Maine that see one there in completely different files.
Well that's what you would use a header file for.
So by making header files you can share this prototype.
So what I want to do is I want you to create new file called test or Hage page for a.
Right.
And we're gonna go in here we're going to go if and death again we're gonna be using the poor process
here test and disco Hage.
And if so what does this mean.
It says if not to find compile what's ever between here.
So if test on Hage is not defined compile whatever is under here and then we just do define test hate.
So this only happens once.
Now what this does is if for whatever reason the same head of file was loaded twice we we you usually
wouldn't want the same code being run twice or three times or being compiled multiple times it can cause
problems in some cases as well such as like cheap cut symbols stuff like that.
So what we do by using this macro here it'll only define it once.
It will only run what is in here once.
So say we define a structure in here and I don't know it has an integer B and then we go struct A B
C then this this ABC will only be declared once rather than multiple times without this if for some
reason the test I hate was included more than once in the same compiler Asian process which could quite
easily happen then it would it would likely try to define ABC twice leading it to being defined twice
which isn't legal.
So by doing if not death test page this will run and then we define test teach and then we know this
code will never compile again it'll only be compiled once and that's what we want.
So you define everything in between here.
Ideally yes get rid of that them.
So we go back to test out C.
So we want my test to be accessible throughout the program.
Right.
So we copy its prototype.
We go back to test H paste that in Okay cool.
So now if we want to be able to access this from Maine dot see we just go to Maine dot see and we go
includes double quotes test dot H.
And now we can call my test like that and this works because what happens is we conclude test out hate
when we can poll means that C and that sees that we we have this.
And if you remember what this means when you just do this without the without without the curly brackets
here while we're saying to the compiler is at some point in time you're going to come across this.
This is what it looks like but we're not declaring it here.
So anytime we access this allow the code to compile because we're going to resolve it later.
And I'll tell you where I'll tell you the code to run for this function later on.
That's what we say when we do this.
When you do this we say OK.
I want to create on my test function we know what it looks like now.
We run everything in between here.
When you say this we're saying I know what this function looks like we don't know what it is yet.
Just store it just allow it allow it to be used and we'll resolve it later.
That's what that says.
So when we do this when you compile test out see it compiles the my test for it here.
Right.
And then when you link the test object and Maine together that's when it finally resolves it later it
resolves my test and will explain more about Lincoln shortly.
So in here it's also good practice to include the head to file.
That's you.
I tend to put it at the top.
What what when you include in something related to yourself.
So it's common practice to include the head of file that's associated with your c file.
So in this case we've created the test I hate to output things to people who want to include the test
functionality right.
But I don't have to include tests I hate because there's no variables in here or structures I'm gonna
be using here.
But it's good practice to do this anyway because it matches the prototypes with what you declared anyway
but it isn't a requirement.
You don't have to do it but it's good practice.
Obviously if you if you had like structures in here or whatever that you'll be accessing inside this
file then you would have to include it.
Right.
Okay.
That was a mouthful right.
So that's how you create the header file.
And now what we're going to do is we're going to actually compile our program and I'm going to explain
the linking process to you.
So let's do that now.
What we're going to do.
Go to have a will there.
And what we're gonna do is reps.
Well we're gonna do is we are going to open up command problems go S.D..
Right click.
Good.
And now we're gonna do things a little differently now.
Firstly we're gonna compile it the way we have been doing it just so you can see that there's an error.
And I want you to see this error so I can explain it to you.
So if you go GCSE main dot c dash Main and there should be a problem with this.
Yes undefined reference to my test.
OK.
Now I'm glad this happened because now I can explain this problem to you.
So remember main includes test Hage.
I remember I was saying that when we do this without the curlies we're basically saying that the compiler
allow us to use it and we'll tell you what it is later.
Remember we'll resolve it later.
I said well guess what.
We didn't resolve it later.
So this is the link complaining that we haven't resolved this that we.
We said we were going to resolve.
We never did.
That's the link complaining right.
So undefined reference to my test is because it can't find the code for my test because we haven't told
it where it is.
So how would you do that.
Well we first need to compile test of see before we compile may not see this right now we compile any
mean that C say so you use a different flag in the compiler for this.
Do GCSE and then I want you to go dash C and when you test out C and I want to go dash o test o press
Enter OK.
Now you can see that what's happened here is it's compile tests I'll see and it's output it a follicle
test or 0 which is a compiled for a compiled object file and what dash C means is a means to create
an object file.
It means we don't want to create the full program.
Right now we just want to create a part of it.
So create an object file and we'll do something with that object file later.
That's what this means.
So if you click on here now it's going to say that by binding file you want to open it.
I'm going to press yes you can see that so the compiled data.
Right.
So it took test to see it compile this function and it put it into test.
And right now we don't have a fully complete program because our programs currently composed of an object
file.
So now we need to link that with main dot C..
And once we link it would mean that C everything will be resolved.
We'll know where my my test is and everything.
So how do we do that.
Well then we go GCSE main dot c space test.
Oh space that show Maine and press enter.
And now if you run main you can see.
Hello how are you.
So let's explain this briefly this final compile line here.
What we say is OK we want to compile mean that C you're going to need to look in our test to o object
file the compiled object file you're gonna have to look in there for any dependencies the main does
C might have because we've probably defined them in the here and then that's what we're saying right.
And then one dash main means are output file.
You know main dot EMC again.
So that is how that works because we tell the compiler if you just do this without the test what you
know you get that undefined reference again.
Right.
But when you say test oh it will check test O's object file to see if this my test symbol is in there
and if it is it knows the code to run whenever you reference my test.
And this is clever stuff.
And it also allows you beep beep beep because of the way object files work.
It also allows you to write programs in multiple programming languages and compile them all together
anyway.
It's quite clever stuff.
You know the link is quite good so you can have other stuff here too.
Let's say we have an object file called ABC though you just do that.
Hello.
You just do that.
And what would happen is it would compile mean does see and it would look for symbols in all of these
files.
That's what would happen.
And then finally Tasha Ming gives us our object file our final running running object file which in
our case will be an E XY file because we're on windows so you know that is how that is how header files
and object files work.
And at some point in this tutorial of this course we'll be doing libraries which basically is is even
it is quite clever allows you to share functionality with other programs but we'll get to that eventually.