Learn to program with c - Part 18 - Libraries

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
other than Happy New Year or if you're watching this then it's not Happy New Year then it's making sense but 2016 now just had Christmas and today we're gonna talk about libraries so we're gonna talk about how to create static and dynamic libraries and how to link to external typically dynamic libraries although you can link to an external static library as well so a library is a collection of functions structures and other things program code that is bundled in such a way that it makes it easy to reuse it so why you might create a library is if you've got a live report on math functions you might want to maintain that separately perhaps it'd hold ever changes you don't want to have to keep recompiling it every time you build your complete program that uses those functions so you maintain that in a separate library and only change that when you need to so there's two types of library that you can create and use and they're useful for two different purposes the first one is called a static library and so we had our own math library we shouldn't report it math what we're gonna do in the example we're gonna create kind of wacky math library so say we heard Liv wacky static library would end in dot a which stands for archive and a files are managed by the tool they are are and what you're notice is what you might notice is is that AR was actually a precursor to the tool tar so if you ever use tar on unix which is for manipulate entire archives then AR was the precursor to that and essentially an archive is just it's gonna have some objects in there so maybe this wacky oh and it will also have like a file that's invisible when you list the files but it will be what's called a symbol table which will a list of all the functions that in all of your object files so an archive file which is a static library just contains a bunch of object files and it contains a simple table with lists all of the functions that are in those object files now the purpose of a static library is to bundle together a bunch of functions and then you can link against an archive file in the same way that you link into an object file so if your program uses the functions that are in this archive then you link against it and then they're included in your program and they're included in the executable of your program they copied into the executable of your program so that grows the size of your executable depending on how many of the functions you use so this is a static library and it's called static because when you link against it those functions are bundled into your program which means they in theory you could copy your program from one Linux machine to another and it would as long as it was the same architecture it would just run it wouldn't depend on any external libraries so that's a static library and the reason you might do this is because there is like ostensibly some speed-up but realistically though it's probably mentioning negligible the main reason is that it makes it a little bit more portable because you don't have to depend on the particular libraries that are on the system and that that's mainly it essentially that you can bundle your program like that but in order to really understand that we need to look at what the other type of libraries and that is called a shared library dynamic library so we might see something like liberace dot so this is all gonna be a wacky math library and essentially in Lib Aikido is going to be the object file in a way it's going to be in there but the point of a shared of dynamic libraries is that it is not bundled into your application so your application executable will not contain the functions in this library that you use they are instead kind of linked into the executable a way that they the code is relocatable so at runtime when you run the executable it looks for this file and then dynamically loads those functions into memory so that you can call those functions so the advantage of this and the reason it's called a shared library is imagine if you wrote had 100 programs on your computer that all use the same library and this is really common like there's networking libraries or JPEG libraries and things which are incredibly common or a zip library is really common and you don't want to copy those functions into your executable for all of your hundred executables because your executor executables become bloated and all so what that means is if you want to if you update library it means you have to recompile all of the programs that depend on that library whereas you can recompile the shared object so the shared library has almost none of the function prototypes has changed and the new program and when you run the programs again it will load that library in typically though people end up changing the function prototypes and have to recompile all of the programs anyway but the advantage is you just got one copy of the library and everyone can dynamically load that at runtime you can get into trouble with this because you can end up having lots of different versions of the shared library around and this is called like dependency hell or something like this where you're linking against a particular version and then when you update your programs you can end up forgetting that you're linking in some particular version and program no longer works and stuff like that but anyway let's not worry about that now let's just look at this first librarian which is creating a static library so we're going to create a static library and we're going to link against it and show you the executable let's go and look at that so we're going to create a static library which means that we need some kind of library to use and we're going to create file then called wacky wacky math door age so there's gonna be header file for our library and in there we're gonna have a single function called wacky add and we were prefix these functions in here with the wacky this is because the symbol names have to be unique when we link against them otherwise the compiler will be confused so this is called wacky underscore add and it's gonna take two integers a and B and it's gonna add them together but that's a header file so I'm gonna create another file called wacky math C which is actually going to contain the functions so we're going to include wacky mouth go H I'm gonna have Y key add a big and what we're gonna do we're gonna return a plus B plus I'm gonna use a function for Rand which returns a random integer and we're gonna modulo 10 so that we get a number between 0 and 9 and because we're using R and we need to include some standard Lib thought page which is where round it's find so ran is only a pseudo-random number which means it generates a sequence of which numbers which look random but they are in fact predictable if you know the seed so that's going to be our function wacky add we're gonna need a program as well which uses that so we're gonna create wacky dot C which it's gonna turn a diode age include wacky man dot H and then we're gonna have a pretty simple program we're actually gonna call s R and so we need standard gauge to set the random seed and we'll set the random C to 0 and then we're gonna call lucky we're going to print out the result of wacky ad three for them cool wacky add the numbers three or four I'm going to do that like four times and then return zero these lines go so that's gonna be our program so what we need to do you know we need to prep make file as as well which I've already done here but let me just remove that so we're gonna have a default for wacky so wacky is gonna be our program wacky is gonna depend on an archive file or live wacky dot a so and there's an automatic make father make for rule which knows how to handle this but just to be explicit we're gonna compile wacky dot C along with the live wacky dot a we're gonna create an out book called wacky so then we define the target for live walkie dot a and what we'll say is that live wacky delay depends on live wacky oh so it depends on wacky oh and we know there's an automatic rule to build o files from C file so we don't have to bother about that target but we do need to specify the actions for this so in order to create liberace a we're gonna use a tool AR and we are going to use our c and v switches are means create a new archive and then add these objects to the archive so create wacky map it wacky delay you know add wacky wacky math Oh to the archive C means to suppress the output saying that we're creating a new archive and V means be verbose which I guess contradict each other in a way but let's do it anyway and so I'm what we will do is we're going to create live back eat or a so using the automatic variable which refers to this name and we're going to include all of the object falls so we're going to use that now this should actually work so when we run make we're going to try and build the tacit work to target wacky the tacky bargain the the target wacky which depends on the Blacky door a so we'll go down to build labaki door a build wacky math Oh and it knows how to build that you know just you'll see we'll show you that when I ton make once that's built it will then run a are to create the archive for lip wacky door a which means it look wacky door it will exist then there's how to compile wacky and produce the output so let's look work so here we are the first line was creating the object for wacky matter Oh as I said it knows how to do that we've then created the archive loop wacky dot a and and we're including the object file wacky math dot o then we've got we're using clang to create our output files let's have a look what we've got so we've now got wacky and we've got lip wacky dot a if we run wacky you can see that we're adding three and four and we're adding a random number between zero and nine to that number to that result so 7 + 0-9 and whenever we run it its identity we get the same output sequence because as I said it's in a pseudo-random and it depends on the seed we change the seed compile it again so nothing's happened because we've not said that wacky depends on what you don't see now we haven't recompiled lip wacky you see so because we didn't need to you can see that the sequence now is different because we change the random seed so we've there we've created a static library called labaki door a from our wacky math files and then we're linking against it here to create our out before so let's just have a quick look at what in these files so using the AR tool we can also list the files that are in a an archive and if we don't use the verbose switch you just get the file so we can see that that's all that's in the archive just that object file so it's quite straightforward and interestingly we can also use another tool called nm which lists the symbols the symbols in an archive file because there is another there is actually another file inside this archive which is a special file which contains a symbol table which we can't see but if we use nm we can see that nm contains one object which has the following symbols in it it has the following functions in it it has the symbol R and because if you remember we did actually use round in our wacky add function and this u means that it's undefined whereas this team is it is defined and its global and there's a symbol called wacky ad so we can actually see what functions are defined by a in by the objects in a library like that using the tool n M of course you can read the man pages for n m and for a R we can also do is list of symbols in wacky in our actual program you can see there's a whole bunch of stuff that is kind of you probably didn't know was in there and you can read all about this stuff and it's quite complicated to structure the program but well what you'll also see is that there's a function called main which is a global function so it's defined and it's it's part of the simple table of our executable wacky and you'll see that wacky ad is also now in the executable so when we swim we link our executable when we when we build it wacky ad is actually copied into the executable wacky and it's part of its symbol table so that's the meaning of static it is statically built into the executable and it's available in that executable it doesn't have to be linked at runtime or anything like that okay so that's how you create a static library and of course if we wanted to I'll just show you one extra thing we don't have to explicitly linked to the file the archive file we could use the - L and then type wacky and and then if we do this I need to change the file just let me change the speed okay well we've got this error cannot find l wacky and that's because the compiler doesn't know where to find it so I need to add one extra thing here I do this other flag this capital L means look in these directories for the libraries so there we go so I said look in the current directory for the library and when linking against wacky and it knows to prefix wacky with lib so it will actually look for live wacky and look for a dot a dot so file in this case it's found at a file and knows how to link so typically when you're linking especially against shared files somewhere else on the system maybe they're in use a local Lib you'll use these else which is to define directories to look in and then you'll just use L and then the name of the library so if we were to look in use a Lib you'll see there's lots of files here like live end also live end or a limb lib kv end or a so this is a static library for KVM system and or a static library for the math system so we can link to those just by using the - L prefix but as you can see all these dot a files and box o files they just exist on the system and so we did a link to them the same way we exactly as we've done here like you know make fall so that's it that's how you create a static library and link to it so it's not that difficult is it and you can of course link to any of the static libraries that are available in your system compile them into your program and what we've also seen is though that a static library can depend in if we list the symbols in a static library there may be undefined symbols so we might even though we're statically linking all of our libraries we still might have those libraries themselves might depend on some dynamic library or in which case they'd still be loading those at runtime right now so we looked at static libraries and now let's look at the dynamic library so let's just look at how to create a dynamic library and then link to that so for I don't know me like we're not actually gonna change anything so we're still gonna if you remember the program we've got our wacky math which just adds the two numbers together and calls ran only gonna change actually the make file to create a dynamic library and then link to that instead so let me open the make form now in order to make things a bit clearer let's create two targets we're gonna have one key static and wacky dynamic so we're gonna create two programs so we'll change this target to worki static walkie static is still gonna depend on Limbach Eid or a and like you don't see but we're not going to use this notation so as I said if you use this - he'll it will look for a library called lid wacky so or live wacky door a but it will do it in that order so we'll look for a Liberace so first so this will actually always compile always linked to the the dynamic library if it exists so we're just gonna explicitly create a static library by that we'll use the automatic variable to refer to these and our output is going to be wacky static before if I before that let me just comment out like Edina now what we get here if we try and do this so I'm just want to illustrate a point here we've got undefined reference - wacky add when we're trying to come back compile this and we didn't get that before now the reason that we've got this is because clang the order in which we supply the arguments to clang matters all we're gonna say at this moment is that the libraries have to come after the C files and the order does matter and sometimes it becomes a problem but typically you just put the library after the C file so I'm gonna do that here then it will work you can read up all about how the linker works and how it resolves the order of objects in the past files but sometimes it will matter so you will have problems like that where you're you're looking like well then providing the object following providing the library what does it mean when the funn reference and pretty much that will always unless you unless the object really isn't in the library it will always be something to do with the order that you provided the libraries so if we've just now changed it so that the library is now in the last sort of thing and type make and we don't get a problem and let's just check in that are wacky static program runs fine so we've now got a static program fine and we've pointed out that the order in which you provide the the library matters I has to come last because those the linking step is last and these things are resolved in a certain order let's create then a new target good whack you dynamic and we're still going to take wacky dossiers name but we're going to say that we depend on labaki dot so the shared objects typically we wouldn't do this if it was like an external library because we wouldn't be building it but we are building it in this case and what we're gonna do is we are going to Rakhi dot C and we're gonna use the notation that we're looking for a library in the current directory the libraries for wacky our output is going to be wacky dynamic so we need a targets to build labaki so it depends on wacky math Oh as well and what we need to do is we say we're creating a shared library and the shared library is called live wacky so and it depends on wacky man oh boy it contains math dodo so let us we're just going to compile our wacky now we're going to get an error and I've done this deliberately so summer look what the error is so we try and create the shared objects depending on wacky math dough and what it says is relocation our x64 x86 64 pc 32 against this object cannot be used when making shared object read compiled with F pick now what this means is that the code is not relocatable so when we create a shared object file all of the code in there all of the functions need to be called what's relocatable this means that they can be placed anywhere in memory this is because the object for the shared library might be loaded in anywhere anywhere in memory at runtime so in this case we just do what it said we need to create wacky math Oh as a relocatable object and we were before this was compiled automatically using make files or one of the automatic rules so we need to explicitly create a rule to make sure that this is done correctly so our wacky math dot o which depends on wacky maxie and wacky math well H we're going to compile this using this and we print an object file so we used I see we our input is working like Dorsey and so let me just remove wacky math again so now we create this working map Doh object using the F peak flag which means to make it relocatable code now we can create the shared library labraccio using that object and finally we can then link compile our program link against Liberace so and this will find the live walkie does so in the current directory and create the object file wacky dynamic right so let's just try and run that dynamic now what we get is this error shared object live wacky door so not found this is because when you execute a program it looks in certain locations for these Esso files and if you read something called an LD config this is how the shared library cache will the share the system-wide shared library cache is created so LD config is very powerful tool so you can use LD config to add your library to the system library can cache and then it will find it it's basically a bunch of directories that it searches but there's another way to do it what we can do is we can set the environment variable LD library path and we can equals to to the current directory or whatever directory we want and then at runtime the linker the dynamic linker will look in the current directory for Lib Waki so now if we we run wacky dynamic it works so we've observed a couple of things here to get our dynamic library to work we had to use F Pig to make the object for relocatable in order to compile a dynamic library a shared library with that object file then at we had to use this capital L to say look in the local current directory for so far we observe that the so file is always linked first so we can only use this to link against in a file if that a file is the only thing if there's a so far that we'll always link there is a dot shared - shared flag that you can pass to clang but that will try and create link every library is static rather than just the library you want there are ways around around that and to try and make it linked to only one library statically and the rest dynamically but it's quite complicated to do and a bit of a mouthful and finally we had to make sure that when at runtime our program knows where to find the shared library and we can use a library path or we can use LD config to configure how to do that and FreeBSD is particularly easy to rebuild he'll be configured on the system's you can completely mess up your system because there's a by default I think here it just over writes the library cache with the directory we provide on some other systems and which means that none of your programs will run after that and you have to rebuild the whole cache which can be quite difficult if you can't run any programs anyway so that's how you build a shared library and how you link against it so let me just build wacky static as well just check that it's built and what you'll note here is that we can build wacky static and we can we can build lib a keyed or a against the dot o file even though we can Paul dado fault with F pink like it doesn't matter that that's relocatable would you still build a static file I'm wacky static is going to produce the same output as way dynamic but let's check will use nm and we'll look at wacky static and we can see that wacky add function is global so it's part of the executable wacky static whereas if we look at wacky dynamic we can see that wacky add is undefined it's an external library which is part of um Liberace so so if we look at live watch so you can see the wacky ad is globe locks its partner share library and the same just with wacky delay but there's some slight differences so they go that is how you create a shared library and link to it alright we've seen both how to create static and dynamic libraries now to look to them and so what we'll do now is let's link to a library that we haven't created so we're going to link to some existing library on our system because that's perhaps a more common use case just using someone else's library that they've written so look at that so let's go and link to an external library what I'm going to do is we're gonna create an STL program so STL is called a simple directmedia layer and it's a library for making games things are doing multimedia stuff so you can draw to the screen so on it says it here is used by some valve programs so it's a pretty cool library and it's already installed on my system on your system you might have to install the dev package but you can look at that so if you're on Linux you might do apt cache search STL pipe grip dev and see what the dev packages and otherwise install the library because you need the header files and you need the library in order to link to it so let's go ahead now what I've got here is a very very simple program I just copied this example here so there's the credit for that and of course in this program and record it STL example dot C so the first thing we do is include the header STR header and you'll quite often see this that the header it file is actually in a subdirectory and that's ok so if we're it will search for s the other H within a subdirectory stl within the include search path which we will look at in a second so there's a very simple program a creek reg to STL surfaces a bitmap and a screen originally there no these are pointers to those surface structures we initialized STL using this function then we set the video mode to 640 by 480 pixels depth of 32 bits and it's a surface type within load this bitmap this is in Windows bitmap full format and I've put this in the directory this mushroom picture and we then blit the surface the bitmap to the screen so we're writing we're drawing the bitmap on to the screen so our mushroom picture then gets drawn on to the screen then we update the screen so call STL flip and this is like lot in a lot of graphics programming what typically happens is they use what's called double buffering so you write off-screen and then you flip that onto the screen so you never write directly to the screen that's quite common so that's what's happening there we're just drawing essentially the screen to the actual screen so we can see what we've written we then wait 2 seconds so that we don't just immediately exit so we can actually see the picture and then we free up the bitmap service and quit so it's a quite a simple program so how do we compile this well let's open up on make file I'm what we're gonna do is we're gonna use the same make file we're going to create another target called STL example and our STL example he's gonna depend on STL example dot c and we need to compile it obviously so we're gonna have the inputs and we need to specify the library and the library is - LS DL so it's capital SD Ellen you need to know what that is and if I do a low K destiel dot so you'll see that that's what it is you can you can go and look for it if you don't know the name it'll usually just be the name either in capitals for a library or in non capitals and you'll be able to find what it's called it says live sto look so we we know that it's in this directory so we need to also say that we want to look for it in user local Lib so I mean to say where the library is that we're linking to and there's something else as well but closed in STL example we will I'll show you I'll show you so that's our compilation for SD SD l example and then let me just try and make that target to show you so we get an error which says we cannot find the header file so here is another flag that you can pass to a command command invocation of your compiler which is a - I which specified include file locations and I know that that's actually in so SDL dot H I have a look for it is actually in user local include so we need that directory so we can put - I use a local include and now the compiler will look there for the header file and we've actually compiled our output currently so we found the header file we're able to include that compiler program we hope to find the library and by specifying the library postfix so the name the library without the Lib and the directory way to find that library let's just run that and see if it's worked there you go there's the mushroom which displayed for two seconds so that's an example of how you could link to an external libraries so it's not really that difficult you just have to provide the library name where to find the library and where to find the include files so whenever you look at a very complicated compilation line like where there's some big program being compiled it'll just be a ton of these so ton of these else statements a ton of these I statements and a load of these statements or statements I don't know the clauses or something basically specifying all the libraries that need to be linked to in that so you hopefully now you won't be intimidated when you see like big compilation things one thing I will point out is a lot of the time when there's a library like SDL installed on your system these days quite often they'll be a tool as well called - config after like SDR - config or gtk - config and a lot of libraries do this these days and this tool actually helps you to provide this information so if i just type STL config it'll tell us what we can actually ask for and we can ask for the lips or the sea flags or the static libs so let's have a look what happens let me just clear it if we ask sorry if we ask for the libs then we actually get the commands that we need to pass in these are a little bit more involved they've kind of provided some extra information here and they're also linking to the threading library as well which we didn't get a complaint when we did that so clone was over to sort of sort that out but what this is saying is that really we should include that pthread information we should be linking our STL example now has the pthread part but going back to STL config so if we ask for the C flags it tells us that this that's where we find there's an include path or that might be the include path so it doesn't necessarily know whether the STL dot H is in a subdirectory or sometimes you might be both so SDL config or the conflict program for a particular library gives you the kind of the full set of flags that you should use when compiling it so here there's a couple of defines as well which one looked at yet but these can essentially switch on and off macros and and well switch on and off defined blocks within your code and that's what they've done here great well that's it we've shown show you how to create static and dynamic libraries how to create your own ones and how to link to them and also how to link to listing static and dynamic libraries that's all there is to know really about libraries in order to use them I mean there's a hell of a lot of detail about how archive files are created how the structure of shared object files and things like that but it's not necessary to know that information to use it hopefully now you can go and use any library that's out there and start building the program's you want to build all right thanks for watching I'll see you next time
Info
Channel: Ashley Mills
Views: 28,923
Rating: 4.9524751 out of 5
Keywords: c programming, external library, shared library, static library
Id: Sy9rGuvfmO0
Channel Id: undefined
Length: 38min 3sec (2283 seconds)
Published: Mon Jan 04 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.