So we are now going to explain what a library is.
You have probably played some games in the past and if you is ever curious some went into the game folder
you would have likely saw a lot of DL files.
These are dynamic link libraries.
These files are libraries and allow the game developer to draw pixels to the screen and add sprites
into that game and also play game music to libraries allow you to share your work with those of other
developers they can use your library and run your functions from their own projects.
We will now be creating our very own library that has a function called my sum that adds to integers
together.
OK let's now create a library so I always do as I want to create a new file.
I'm going to call it my laptop see and the name of this c file doesn't really matter too much.
However we're going to call it my laptop C so do inch.
My son.
Just a chippy son A plus B.
So obviously this is just going to add a A and B together and return the value.
Now appear to include my lib.
Hate each.
I'm going to create a new file called My lib da Hage
and we're going to go if not def my lib underscore how each defined my lib page
and if and then here just to integer mice into a and should be so obviously by putting this in I had
a file we can allow the other C files to be aware that this function exists somewhere but we don't know
where yet and we'll know when we link the program together.
Okay great.
So now what I need you to do is I need you to compile my laptop C into a library file so how we do that
is we get up our hello world app again we see D into it in command prompt
and now we're gonna compile it but we're gonna.
We're gonna compile it a little differently this time I want to do GCSE my lib dot c dash so my lib
dot D allow for dynamic link library and I want to do dash shed press enter.
And we've now created our very own Chad library and that's all compiled nicely.
So now what we need to do is we want main dot c to be able to use that function in my laptop to allow.
So what we can do we can go inside here we can go include my laptop Hage and then we can go into a equals
my sum five and five print if I may and I would compile that we'll see a problem
undefined reference to my son.
Now that's because we need to link it with my laptop to allow.
So you can do that by doing.
May not see my laptop yellow.
And now that's compiled.
If you now run main you'll see 10.
Now let me open the project directory again to show you how this works.
In this scenario we have not combined my sum into this into the main excel file.
We haven't done that in this scenario.
What we've done is my laptop does allow a separate from main Daddy XY but mainly I see relies on my
laptop to allow.
So if we just delete my laptop out now and we try to run that again.
What's this the code execution cannot proceed because my lib dot DLF was not found.
So what has happened is my son was in my lab at the yellow not in my main dot you see not in there.
So main do you see now depended on that file and because we've deleted it we can no longer run the program
because maned xy is not aware of my son my son was stored in my liberty yellow so that's what makes
them different to object files with object files.
We can but we compile all the object files to make a program so main would mean that you USC would contain
all of the symbols it needed including my sum but because it's a library it's different.
What we said was is when we compiled it with shared we basically said create a shared library a dynamic
Dynamic link library and store store to store our symbols in there.
And then when we went and compiled means out C we provided my little DSL which is telling the compiler
that this program depends on this on this library and that you can resolve symbols there.
So now we need make my laptop to allow to run this program because my laptop to allow holds my sum and
not Main w XY main WFC does not hold my sum so I hope that explains libraries essentially in in like
a game project you would have all sorts of graphic libraries dynamic link libraries.
So the whole point of libraries is basically just to extend functionality for different projects and
allow them to reuse code written by another developer.
Compiled code so that is how it works.
However just to further explain that these are two separate things.
My lab and main what we can do is we can change.
We can keep the main file the same and we're not going to weaken pilot by the way but we will change
my laptop see let's put a plus five by here as well.
So my son will now some a and b together and a lab plus five as well.
Now if we we can pull out again JCC My Laptop see Dash show my lip dot the yellow dash shed press enter
and now to main again.
It gives us 15.
Now we haven't changed the code of main dot see a toll.
We haven't re compiled main dot see the executable has not changed but we did recompile our library
and now knows how the value is different so that proves that my sum is in my lab and that it is not
compiled into main Ducky XY.