 Hey everybody, this is Brian. Welcome to the 97th Qtutorial with C++ and GUI programming. Today we're going to discuss dynamic link libraries. What are they and why do you need them? And more importantly, how do you create them? So a dynamic link library. If you go to Wikipedia and look it up, you get a very long and exhaustive description about this. I can probably save you a little bit of reading. A dynamic link library, think of it this way. You write some code and you want to distribute that code to everybody you know. But you don't want them to modify the code. You just want them to be able to use it. That's what a dynamic link library is. It's a container that allows you to put your code into it. It creates a DLL file. I should probably back up. You may have noticed I'm using a new version of Qt Creator. I'm now using version 2.3.1. Some people have asked me about some differences here. But anyways, so a DLL is just a wrapper around your code. So if you go to File, New Project, Other Project, C++ Library, you'll see there's a couple different types. There's a shared C++ library for use with Q plug-in loader and a shared or static C++ library. The differences between the two, I'll let you really research those. But a Q plug-in loader is obviously a Qt class. It needs a couple little extra things to run whereas a normal C++ library is just very bare bones minimal. So let's just do Next. And for this, we are just going to make a shared library. If we use Q4 plug-in, that's actually with a Q library loader. So let's just call this mylib. And let's put this in the usual location. So another thing you can use with a DLL is let's say, select required modules. If you wanted to use different types of modules, it's just a friendly GUI for you to choose. We're just going to leave it with Qt Core. Everything else is just standard. Anyway, sorry. Let's back up. You have a piece of code and you want to use that code multiple times across multiple applications. You have two choices. You can either create a library or you can just copy and paste that code into every single project you make. Well, copy and pasting isn't very efficient. So we'll make a library. Go into your project file and you'll notice how the template is now equal lib, lib short for library. And you've got a couple of different little things in here. Don't really pay too much attention to this. These are phone settings which we won't really be using. But you should really note that the template is lib and it is minus the GUI. The source file is pretty bare. And we have two header files. Let's open up mylib.h. All right. Now you see how it's including this mylib global.h, this other header file. We'll get to that in a second. And it's saying class, mylib shared export. What in the heck is that? We haven't seen that yet. Well, if you jump in here, I'll give you a very brief discussion about how DLLs work. When a DLL is compiled, it creates what's called symbols or little bookmarks within the source code. Or I shouldn't say source code, within the binary file because it's compiled. And what this does is this tells it that we are exporting that class. And it can also be imported by other programs. So we have to tell those other programs where the symbols are and what we need. If you paid attention during my video on basic C++ with header files, now you'll start to understand why you need to separate your header from your code. Because your code has the actual implementation. This will be compiled and inside the DLL. Your header file is what you're going to distribute to your buddies who are going to use your DLLs. So let's just go in here and we're just going to create a function. Same lib. Let's just call it test. Just keep it simple. This tutorial is focusing on how to create a DLL, not how to write a bunch of code here. Refractor, add definition to. And there you go. Create our little test here. Let's add in qdebug. And our test function is just going to be very simple. Very easy. We're just going to say qdebug. Hello from our DLL. Now, if you're in Linux, because remember Qt is cross-platform. It won't be a DLL. It will be a .so. I think that's stood for system object, but it's called a library in Linux. So really all our test function does is it just spits out a qdebug. Hello from our DLL. Now, draw your attention down here. We haven't really talked about this whole lot. Notice how there's a release and a debug. Flip it to debug and then wait for it to evaluate and parse. And let's give it a good build. You see in your compile output it starts doing some stuff. And then it says, Ming32 make exited normally. And you can kind of scroll up here and see how it's doing a whole bunch of stuff. What it's really doing here is it's compiling your code into a DLL, creating object files and linking it into a DLL structure. That's where all the symbols get made. Now, if we go out to our file structure, you see we have our source code. And we have our build directory. And we have our debug. There's the a file, the object file. And here is our DLL, which we've created. Notice the size is 437 kilobytes. That is huge in terms of file size. So let's go back in here. Let's flip this into release. Wait for evaluate and parse. Give it another good build. And let's just jump back here into our release folder. Something happened. Something went boom boom. Let me pause and figure out what happened. Sorry about that, I was in the wrong directory. Here we go. Go into release and there it is. Mylib.dll is now 78k. So what is the big difference? I mean we're talking about 300 kilobytes here. Well, debug remember has a lot of debug information, which we haven't really talked about debugging too much. Debugging is how you step through a program to find errors or flaws in your program and trace variables and figure out what's going on in case things run amuck. Most of our projects fortunately have been pretty error free, so we haven't really had to focus on that. Now when you create a DLL you should compile and distribute a debug and a release version. The reason for that is people are going to want to use your debug or use your DLL in debug and release modes. Pretty simple stuff. Now what do you distribute to your end users? What they're going to need are your two header files and the DLLs we just created. And we're going to cover that in the next video. But I want to back up and make sure you really understand the concept of a DLL. What we're doing here is we're taking this code and we are wrapping this inside of a DLL or a library file. You do not distribute this code to your end users, you only distribute the headers. So this is what your friends, family, coworkers are going to see is this header file right here. They won't know the contents of this test function because this is going to be inside the DLL itself. So if you want to write some code, sell it to somebody, you want them to be able to use your code without actually having access to it. DLL is a perfect platform to do that with. Companies do that all the time. Now why would you use a DLL? That's a common question. I get tons of people asking me, hey, I want to learn how to do a DLL and then their next question is, well, why would I really make one? They kind of know it's important, but they don't really know why. The reason why is modularity. You want to be able to write a function once and use it hundreds, if not thousands of times across dozens, if not hundreds of your other programs. So let's say this test function actually did something really cool, like calculated, you know, how long you have to live, like a death clock or something. That's kind of more. But anyways, it's getting close to Halloween, so bear with me. You'd want to be able to use this test function in every program you made. So you just distribute the DLL and the header files with it and you never have to touch this code again. And likewise, you don't have to worry about somebody modifying your source code and screwing it all up. Well, that's all for this tutorial. Thank you for watching. I hope you found this educational entertaining. In the next tutorial, we're going to be building off of what we just created. We're going to actually build an application and use our DLL. So stay tuned.