Using Dynamic Libraries in C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys my name is the chana welcome back to my estate plus plus series so last time we talked about how you can actually use libraries in C++ and your C++ project and specifically how you can link them statically but today we're talking all about dynamic linking what it is how to use it when you should be using it more that stuff and this time we're going to be linking JFW dynamically so in the static video we linked it statically and now we're gonna link it dynamically and kind of see what what was involved in that and what the differences are so first of all dynamic linking what what does it mean how is it different than static why is it called dynamic so dynamic linking is linking that happens at runtime so static linking happens at compile time when you compile a static library you then link it into either an executable an application or a dynamic library and that's kind of it you're done you literally take the contents of that static library and you put it into black with the rest of the binary data that's actually in your dynamic library or inside your executable and because of that there are there are a number of optimizations that can happen because the compiler and linker is now fully aware of the code that actually goes into an application when you're linked statically when we talk more about the actual performance differences in what Ward linking statically versus dynamically actually means in a future video this is just a basic overview but just keep that in mind that static linking kind of allows more of that optimization to happen because the compiler and linker kind of is just they can see more of the picture specifically the link that can see more of the more of the picture whereas dynamic linking as I mentioned happens at runtime what that means is that when you actually launch your executable that is when your dynamic link library gets loaded so it's not actually part of the executable when you launch normal executable it gets loaded into memory however if you have a dynamic link library what that means is that you actually link another library and external binary file dynamically at runtime so you run your application and then you load an additional file into memory now the way that executables work they can actually require certain libraries to be present certain dynamic libraries certain external files to be present before they actually let you run the application that's why on Windows for example you might sometimes see when you launch an application you might sometimes see our message pop up being like blah blah or DLL is required or is not found so we can start the program and that's one form of dynamic linking that's kind of I like to almost call that kind of 50/50 because they're executable is aware of the dynamic link library and it actually lists it as a requirement but it is still a separate file a separate module that is loaded at runtime you can also load dynamic link libraries completely like dynamically so the executable can have nothing to do with it at all you can launch your executable your application it won't even ask you to include a certain dynamic library but then inside your executable you can actually write code which looks for and maybe loads certain dynamic libraries at one time and that obtains function pointers or whatever you need to the stuff that's inside that dynamic library and then uses that dynamic library so we don't have libraries just keep that in mind that's kind of the static dynamic version which is basically my application actually requires that this dynamic like link library be present and I'm already up I'm already aware of what functions are in it and what I can use but then there's also I want to arbitrarily load this dynamic library I don't know what I don't even know what's in it but I want to pull out some stuff or I want to do many things with it and there are very good uses for both of them we're gonna well we're actually gonna focus on the formal one for today which is I know my application requires this library but I'm going to link it dynamically set of statically so let's just jump into an example and look at what that looks like for GL of W because gel W actually lets us link either statically or dynamically so a lot so in that static so in that static linking video we basically just linked our jail it'll be statically we included the header file and we call gob in it if I run my program you'll see that it compiled successfully and it prints one as our result here now not everything is going to change if you link dynamically this include for example remains identical I mean the header file supports both static and dynamic linking there are actually differences that need to occur with your declarations for things like functions if you want to link statically versus dynamically and we'll explore that in a minute but G love W like most libraries actually kind of supports both static and dynamic linking with that same header file as we'll see in a minute so this doesn't change and if I right click on my project you go to properties you'll see that under CC busbars general I have that include part again if you know 100% sure about how to set all of this definitely watch my last video on static blinking even if you're only ever interested in dynamic linking you still need to know what static linking is because a lot of times you'll actually probably want to link statically if you can instead of dynamically so check that out if you're not sure this doesn't change and then if we go into linker input we see that we have GL fw3 Lib so if I open up my folder with GL w now just right-click here get open folder and file explorer and then over here in my file explorer I'm just going to go back here into dependencies jail of W and Lib so you can see here that I've got three files jail also p3 don't live which is clearly the one that we're actually currently linking the static library let's go ahead and get rid of this one and replaced it with the dynamic version if we go back over here you can see clearly that we have to kind of dll files well one one of those actually called jail that we three dll Lib that one is just basically a series of pointers into kind of this dll file so that we don't have to actually retrieve the locations of everything at runtime it's very important that these two are compiled at the same time because if you try and kind of use a different static library to link with a dll at one time you're probably going to get functions mismatching and wrong kind of memory addresses for function pointers and just it's not gonna work out basically what I'm saying so this of course is distributed by jail fw so they were compiled at the same time and they are related directly to each other that you can't separate these two so back over here I'm going to type in jail of w3 DLL lib and that's all we have to do from this side now if I hit OK and I try and build this it's going to work successfully right so you can see that it's generated my executable file fighters remove this over here I don't really cares about the errorless anyway output is where it's at I'll move this over here you can see that HelloWorld exe has successfully been generated let's try and launch our application now all right and you can see that we get this error message that I talked about earlier the code execution cannot proceed because Java b3o tol was not found this is where we actually have to show our program this is Jill Toby thread of DLL right I've got it over here please Lord and the way that we do that in a simple case is just basically placing that dll file in the same location as out executable so if I got makea I can copy this dll file go back over here interdependencies HelloWorld debug and you can say this is the path with my executable if I paste this in here and I go back to either visual studio I could run it from there as well and I try and run my program you can see that it works successfully and we actually get one over here all right beautiful if I go back over here to my actual Windows Explorer I can also double click on polar world and you can see that it runs just fine in here without bedroom studio without any kind of debug is attached as well so that's all there is to it we link against the static library and then we actually make sure that we have the DLL in and in an accessible place you can throw out your application actually set paths to certain libraries like search locations but the root folder of your executable so the folder that actually contains your application is just automatically kind of a search path so if you put it into the same folder if you put your DLL file into the same folder as your executable you'll be fine okay so one more thing that I wanted to mention is if we take a look at this actual header file we can kind of start to see the differences between what happens during static and dynamic linking specifically if we look at pretty much any function over here you'll see that it defines geo wapi before the return type and then the actual function name so if I go to the definition of that and there's a few definitions lost click on the first one you can see we have this whole thing here we've got this case for each we're on Windows and we're trying to build the DLL file then it has to actually export the DLL functions this is actually really important if you build it without this it's not gonna work then there's also win32 and just gelato BDL which means we're calling here shall be as a win32 dll which does deathless bacterial input and then finally there's also the building the static library and then calling a GL of dopiaza static library which is to find the show w api is nothing so that's actually what's being defined right now now here's an interesting question we are still defining Jeff w API as nothing even though we're using it as an actual DLL so shouldn't GLW dll be defined if we go back to the main if I go to properties over here and I go into my c c++ preprocessor and then I add that progresses the definition gfw dll is try and compile the code one more time okay seems pretty legit here it is let's hit f5 and it runs and it looks like it runs in exactly the same way so what's going on here why did I not have to define deco spec DLL input why did I have to just like even nothing works and why is that and that is a question for you guys you've been asking me for a while to give you kind of challenges and things to do for homework or whatever is a great example why is this happening why am I able why do I not have to actually death respect the other import to be able to link to these functions in my DLL file successfully leave a comment below whoever wins this will just get pinned as the comment that's you guys a pretty decent prize I'm alright but yeah just leave leave a comment below and we'll see who can figure it out anyway thanks for watching I hope you guys enjoy this video if you did you can hit that like button any more questions about dynamic linking about static linking all that stuff leave a comment below as well as hop on my discord that which is the channel to come such discord there's a bunch of people there talking about pretty much everything to do with C++ and OpenGL and graphics and programming and just everything it's a really good time if you really like this video and you want to help support the series that I do here on YouTube then you can go to patreon.com/scishow Cherno so guess the pretty cool rewards like getting videos early and lots of other fun stuff so definitely check that out and I'll see you guys next time good bye [Music]
Info
Channel: The Cherno
Views: 135,087
Rating: 4.9075208 out of 5
Keywords: thecherno, thechernoproject, cherno, c++, programming, gamedev, game development, learn c++, c++ tutorial, dynamic library, c++ library, dll file, c++ dll, how to use c++ library
Id: pLy69V2F_8M
Channel Id: undefined
Length: 10min 6sec (606 seconds)
Published: Wed Oct 11 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.