C++ Dynamic Linking vs Static Linking

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
static versus dynamic linking hopefully at this point if you watched several of my videos you know that static means compile time whereas dynamic means run time when we stop to talk about static memory you may think that's that's a variable shared amongst too many classes but the history behind that goes much further static variables or variables that have memory created by the compiler at compile time embedded directly into the executable it's statically created by the compiler compile time whereas dynamic memory new or malloc we create that memory at runtime so it's important to know that static is compile time dynamic is runtime I have here a mu inside of main I've declared mu I have not defined mu there's no body on mu so I can try to invoke mu here which is fine if the compiler is compiling this compliation unit by itself compiler will be happy and leave it to the linker to patch up the empty space here where's mu actually created let me bring up this command prompt and list the contents of the directory if I can get the command line correct we have manor dot cpp it's this compilation unit I've saved it into this C++ code to folder on my hard drive and then we have any other C++ file which I've put over here and notice I i've defined mu I've actually given mu a body and I've also put this declaration specification DLL export on here and and hopefully you're just for review though I've shown this before hopefully there's a review this is a way to say hey when I make a library file or a DLL I want this visibly callable outside that library that's all that means and then hopefully this is old hat using namespace Tanner it's kind of a bad thing to do but this is a simple example I'm fine with it now let's say I'm running my own little software company and I like writing contrived functions that do pretty much nothing except hello world examples like saying move to the screen and I want to sell this to you and so you and I am strike a deal and I sell this code to you for five bucks but I want to give you my code and this is highly confidential secret way of doing things instead what I want to do is give you a library but you could link against compile against and consider but you don't necessarily need my code directly and so let's let me I'm gonna put this in a vertical tab group so you can see these side-by-side you've written this code to try to consume my code and I've written this code I need to package this code up and give it to you with a few ways you can do that and we've seen in the prior videos in this playlist I could compile a bunch of obj files together which would kind of work but then you would have to boy you would have to specify all those obj files when you compile it I'd make your life a headache so what I couldn't do instead is package all of my obj files in this case it'll only be one obj file but I could package them all together in a library file a dot Lib file and give it to you okay and you can link against my library file either dynamically or statically and we're going to see the difference here very shortly I'm gonna bring up the command prompt again and let's say I'm on my end I want to package up this compliation unit and let's first do static linking I want to make a static library and when you link against my library you will do it statically the first thing I need to do is say hey compile and Link but only compile don't worry about linking compile only is what the /c means i'm the other c++ file dot cpp let's run that and clear the screen list the contents of the directory we now have an obj file we've seen this in Prior videos in this playlist this obj file again i could give it to you and you could make that as part of your compiled elation process but that would be a headache let's go a little further and create a static library something you can link to statically and the way we do that kind of a roundabout way of doing it but there's this lib tool for the library that's built in with the visual studio tools available via this visual studio command prompt I'm gonna say library slash out and I'm gonna call it Jamie's Jamie's library have a la Jaime's library dot Lib Lib short for library and the input file they only follow only obj file I want this library as me other C++ file dot obj hit enter looks like it cranked away list the contents of the directory and now we have this this library file I've taken this obj file and pumped it into this library file I have statically put it in there so it's a static link library now on your end you've written this really cool mu consumption code to consume my move function that's residing in this library and just to prove you we don't longer need the obj file I'm going to erase start out obj clear the screen let's the contents of the directory all we're left with is this library I'm gonna compile Mainer dot cpp this file here your file and I'm going to statically link against this static link library so compile and Link Mainer dot cpp i want to link against my and that's Jamie's library dot live hit enter looks like everything compiled just fine let me clear the screen let's the contents of the directory we now have Maynard XE and if I run Mainer dot exe I'm hitting tab there front of completion you'll see oh yes it prints mu and just to prove to you that we've statically linked I can erase this Lib file and what does it mean a statically linked well the compiler essentially copied and pasted the machine instructions inside of this Lib file it it copied them out of there and put them directly into the executable okay in fact I actually want to note the size of the executable here let me get the windows snipping tool up here if it'll open come on snip snippety-snip there you go the snipping tool and I'm gonna just grab this portion of the screen and that's our static linking okay notice manor dot exe ninety-nine thousand bytes justjust let's tuck that away for a little later in the video again when we compile this mannered exe the machine instructions were copied statically copied the compiler linked it in statically it's it's right there in Maynard exe so if you want to change or if I want to change if I want to change my code over here and give it to you you have to recompile your executable again in fact just to prove to you that that code was copied and pasted I'm gonna erase Jamie's library don't live and notice Manor Manor still runs just fine it prints Moo the code is embedded in the executable so that's static linking the compiler linked it it was copied and pasted into there let's do dynamic linking I'm gonna erase let me actually I'm gonna pause the video and just erase all the other files and start all completely over again with a clean slate except our two input files don't blink wow look how fast I am let me list the contents of the directory again we're at the very bare bones beginning we have any other C++ file and Manor dot cpp txt files right now we need to need to turn them into something so this time now that I want to ship my code I don't want to make a static library because every time I make a static library and you compile against my static library then if I change my static library you also need to recompile your code oh what a headache what a headache I wonder if there's a way we could push this process to run time where instead of you linking at compile time and copying and pasting my code into your executable and I say code but it's really the native machine instructions out of my Lib file but either way instead of you copying and pasting that into your executable statically at compile time I wonder if there's a way we could link dynamically at runtime okay static linking versus dynamic leaking we want to link at runtime well we'll watch this I'm gonna say compile and link on my end we need to we need to create something from my cpp unit I'm trying to give you this awesome code here and I'm gonna say LD that's that stands for link dynamically I believe I'm pretty sure it does then the name of the output file /fe I'm gonna call it my dynamic library dot DLL you notice the DLL now I'm not specifying a Lib I'm specifying a dll dynamic link library let's link at runtime and then again it's me other C++ file dot CPP this c++ file with all this in there hit enter drinks away for a while looks pretty good let me clear the screen lists the contents of the directory and notice what we have now notice what we have now we have a dll file a dynamic link file and we also have a Lib okay we also have a live but let's compare the size of this Lib this dynamic dynamic ish Lib with the static one we did before let me bring this back on 1900 bytes and the last one was nineteen hundred bytes last one was sixty eight thousand bytes the Lib file was much larger than the static world because all the code resided in the Lib file is in order for you to copy and paste out of my Lib into your exe with static linking the code the native machine instructions have to be sitting there in the Lib file but in this case with dynamic linking the codes not sitting there the codes sitting in the dynamic link library file notice the size of the dynamic link library file it's much larger than the Lib file alright the Lib file is just kind of an intermediate stop when you compile against my code your link 'red wants to know hey I need to resolve this mooo call somewhere and so it will look in the LEM file and see hey mu its address will be resolved dynamically and it sits inside of this DLL file which will need at runtime so what runtime will finish that up and so at compile time the linkers like okay cool I'll just patch that up to dynamically find mu inside of this dynamic link library at runtime let's compile this code against what we have here let me my cursor back in here I'm gonna say compiling link Manor dot Manor CPP and again we want to link in my dynamic not live I'm hitting tab therefore auto-completion we want a link against that and then a cranks way and everything's just fine let me clear the screen list the contents of the directory so now we no longer need the Lib file unless you want to compile your code again alright that Lib file was only necessary for thinking in the linker saw oh okay at runtime we'll we'll resolve what mu really means here runtime and it will be inside of the dynamic link library and you can see again we have Manor dot exe left you let's actually run Maynard prove this works Manor dot exe e and we get the MU very cool but what happens if I turn around I'm gonna erase my dynamic link library dot Lib okay just like we did before and then manor dot exe e okay still runs just fine because manor dot exe all the executable cares about now is the dll file the compiler worried about the lib but the executable is dynamically linking linking at runtime against the dll file well what happens if I erase my dynamic link library DLL hit Enter Manor dot exe and boom we can't link to that dynamic link library anymore at runtime it said hey I'm um program can't start because my dynamic linker it's missing how am I supposed to run code that doesn't exist we'd no longer copied and pasted it statically into our executable it's trying to link to it dynamically the DLL is gone oh well since we are dynamically linked say you call me up you say ha Jamie I lost that DLL file can you give me a new one oh yes I can give you a new one in fact its new and improved instead of saying new it says new you're like oh ok that sounds good that sounds good so on my end I need to I need to recreate that DLL so let's let's do just that I'm gonna say compiling link again link dynamically link dynamically the output file name must be identical to what we had before my dynamic library dot DLL and then the input file again it's me other C++ file but this time with the new and improved mujer in there so me other C++ file hit enter looks like everything's good there let me clear the screen lists through the contents of the directory we now have our DLL file again new and improve this time with newer in it notice I'm not recompiling your executable if this was static linking I would have to recompile your executable so that when we compile your executable it'll copy the code out of the Lib and put it in the exe but not in the lib just the linking and points for the linker at compile time the compile time linker points are there but we're done with that we've compiled your executable I've just replaced my DLL I've shipped you a new DLL you're stoked you don't have to do any work you don't have to recompile your executable you run your executable hence the term runtime and at runtime dynamically you will link against my DLL hit enter and there you go there's my new and improved DLL coming out more isn't that cool I can compile on my end over here without forcing you to recompile that's kind of the beauty of dynamic linking whereas static linking that's compile time linking it's a copy and paste job of the code and into your executable hence making your executable bigger and making it less flexible of course there's a little bit of runtime overhead by linking to the dynamic library there's not ever been a huge amount of overhead that's really gotten in my way though so there you go static linking versus dynamic linking
Info
Channel: Jamie King
Views: 91,601
Rating: 4.8674464 out of 5
Keywords: Static Library, C++ (Programming Language), Library (Software Genre)
Id: Jzh4ZULXsvo
Channel Id: undefined
Length: 15min 6sec (906 seconds)
Published: Fri Aug 02 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.