 Hey everybody it's Brian and welcome to the 87th Qt tutorial with C++ Inquiry Programming. Today we're going to cover what's called a static compilation. I know quite a few of you have been messaging me saying how do you do this. Well first off we need to explain what is a static compilation and to understand that we have to explain the opposite. So what you've been doing all along is called a shared compilation. And before you I've got this basic application. It's nothing fancy. I mean if we run this it's just quite literally a window with nothing really going on here. Look at the project file and you see that we're calling the core and GUI libraries. And if we open dependency walker this is a software package developed by Steve P. Miller out on Microsoft's website. You can just google dependency viewer dependency walker and get this program real quick. We'll go ahead and open up share test two the program we just made and you can see how it says they were errors. The reason why they're errors is because we're missing these DLLs. Remember that nasty little thing we went over in our last tutorial where when you try to run a program and you don't have the libraries it pops back and says couldn't find library blah blah blah. That's called a shared compilation meaning you're sharing these libraries with other programs. You're not the only program calling the Ming W M10 or the Qt Core 4. There could be hundreds of programs calling these so you're sharing it. It's called a shared compilation. Now a static compilation or a static compile as it's more commonly referred to is where all these things are inside of your EXE. So when you give your executable to your friends your mom your grandma whatever all that code all those libraries are actually compiled inside of your program. So you only have to distribute one file. Now I have to put a bit of a disclaimer here. There's no such thing as a pure static compilation meaning an executable that has zero dependencies. It just does not exist. But what you'll find is that you take your main dependencies like Ming Qt Core Qt you put them inside your EXE and then you'll still need like kernel 32 and MSVCRTD.dll but those will already be on the target machine because they're part of Windows. So you don't need to distribute those. So that's the goal of this tutorial is to show you how to do a static compilation. Now I have to admit I'm not an expert at this and it took me quite a bit of research to figure this out and a lot of trial and error. So I'm just going to throw that out there. You may know more about this than I do and that's okay. If you have some helpful hints share them with everybody. All right first thing we need to do is we need to actually set up what's called a make spec. Whoops there we go. In your Qt directory I have it on C drive Qt 2010.05 Qt make specs. These are all the make specs for all the different operating systems and compilers that Qt allows. Quite a bit. Now because we're using Qt Creator we're using Win32 G++. Now you could also be using Win32 MSVC 2010 2008. This is the Microsoft Visual Studio compiler. So we're just going to go into Win32 G++ and there's this file Qmake.config and you're just going to open that up and it's going to bark at you and say what do you want to open it with? We're just going to choose notepad. I always uncheck the always use because sometimes it just gets annoying and we're going to look for this little thing called Qmake L flags right here. Qmake L flags. Now what this is is this is a flag that tells it how to compile it and you notice mine says dash static. Yours probably doesn't say that because I've already gone through the pain of recompiling Qt because you're going to have to recompile during this tutorial. So go ahead and open that file up that's the Qmake.config in the make specs Win32-G++ directory and just add in dash static and then save the file and you may have all this other gobbly cook back here. Honestly don't ask me what that is because I have no idea what some of that stuff is. Some of it seems pretty self-explanatory. Some of it I just don't know. I'll be brutally honest about that. So add dash static to your Qmake L flags. Save your file when you're done. Then what we need to do is pull up a Qt command prompt. This came with the Qt SDK. You notice how it says setting up a MingW Qt only environment and it has all this stuff it's doing. You have to open this command prompt not a normal command prompt. Otherwise it's going to bark at you and say I have no idea what you're talking about. You're going to type a command called configure and be sure first off be sure you spell it right and do a slash question and that will give you all of the options for configure and there are quite a few of them so you got a lot of reading to do but we're going to pick out just the main ones here. If you scroll all the way up you see how it says shared static. Notice how there's a dot or a little star next to shared because we're currently in shared mode. If you want to do static we have to give it a static flag and you see how it says create and use static Qt libraries. This is really a three part process. You have to update that Qmake.config file. You have to configure Qt and then you have to compile Qt and I'll be pretty honest configuring and compiling takes a long long time. So we're on step two. We need to configure Qt so we're just going to type configure and then we want platform and this tells it you know what compiler to use win32-g++ and if you're wondering where I got the win32-g++ from it's from our makesbacks member win32-g++ that's the compiler that the Qt creator uses and I should say that's the environment. We want to do a static build meaning we want to put all those libraries inside of our executable. We want release. If you forget to use release you may have a DOS window that appears with your GUI applications. So you'll have your main window and then a DOS window up to the side. That's your debug window. So if you do it in normal or just debug mode you're going to have that little DOS window on almost every program and it gets really annoying. Then you want to put in no-exemptions. Sorry EXC. I'm a horrible speller today. I have to apologize and you put in no exceptions because some DLLs out there will prefer to be shared. When it goes to compile it's going to say uh don't add me I want to stay out in my own little DLL and what no exemption says is no you cannot do that you have to be compiled. Go ahead and enter and you'll know you typed it correctly if you get this. Which version do you want to use commercial or open source. Now I have to throw out another disclaimer. Static compilation is a violation of the open source edition. If you read the license which admittedly I haven't read in a while so it may have changed but if you read the license what it says is if you use the open source edition you must do a shared compilation meaning you must distribute the cute libraries with your applications. Why? Simple. They want to market themselves. They want people to know you use that. Also because when you do a shared compilation certain features or sorry a static compilation certain features are disabled. For example plugins are disabled. So if you make an application with a bunch of plugins and you think you're all cool and then you do a static compile it's going to stop working. The reason why is the way memory is treated in the background. So anyways this is for educational use only. I'm just going to type in an O for open source. Yes I'm violating the license at this point if I make and distribute a program so I'm not going to distribute anything with this. Then it says okay what do you want to do? Do you want to view the license? Do you want to view the lesser license? Do you accept or do you decline? We're going to just accept and then it starts just grinding away and you may think you're actually doing something but really you're getting ready to do something. This is only step two. What you're doing is you're creating all the make files so that when we go to compile this it'll all be in static mode and this is going to take quite some time. So I'm going to go ahead and pause the video. I recommend you get a drink order a pizza, watch a movie, something like that. Time for this depending on your machine could be anywhere from 10 minutes to half an hour maybe longer. So I'm going to pause the video and we'll be right back. All right now through the magic of time lapse photography that only took a few seconds. You can see how it says cute is now configured for building. Just run ming32-make to reconfigure, run ming32-make, come clean and configure. What does all that mean? Well means you need to recompile cute. So let's do it. Let's go ming w32 make and we want to put the sub src for subsource. And I will warn you this will take an extremely long time. Give you some time estimates here. On my machine which is a fairly beefy computer it took about 45 minutes the first time I did this. But I also was playing a video game and doing some other stuff too so I'm sure it would take a much less time. On an older computer that I work with a laptop it took about four hours. I mean it was a pretty lengthy process here. So the good news is once you have actually compiled these you never need to do it again. So I'm going to press ctrl-c kill the job because well I've already done this. Now let's jump into cute here. So when you're doing this just let it compile all the way but like I said I've already done this part so I'm going to just skip that and we're going to say static test 01 put in the usual location and notice how nothing's changed so far. I mean really we're just doing the same thing that we've been doing. Now to get this to compile statically we're not done yet. See if we actually just run this and let's let this thing build and compile and all that stuff. Notice how we're in release mode also uh-oh what's this say undefined reference too. Yeah that means we've got a problem with our libraries and we're going to have to recompile. I stand corrected so I'm going to recompile and I'll be right back. Sorry about that I forgot that I deleted the static libraries off my computer so I'm fully recompiled and ready to go. So what we need to do next is do a go into your project file and we're going to add an extra line here. We're going to say config say plus equals static. This is how you tell it you want the static libraries. Let's just save this. Let's give it a good build just for good measure. Notice how it completes successfully this time and run it and there's our window. Now let's jump into dependency viewer. Here's share test two. Let's open up static test 01 release. There it is. You notice how this thing is eight mags in size. That's how you can really tell it added all those libraries in there. Now when we open this up into dependency viewer it's going to go through and it's going to read it and you can see how we've got a bunch of DLLs here that it needs. Let me collapse all these. But you're not seeing the the Qt libraries at all. So where are they? Well they're all compiled inside of the executable. You notice how it needs all these other DLLs and drivers and stuff like that. Well these are all included in Windows like kernel32.dll. That's part of Windows. Microsoft Visual C runtime that's part of Windows only 32. Once again part of Windows, shell32, etc, etc, etc. So you can now distribute your program without the need of any extra libraries. So let's go out and let's actually look at our program here. Here's static test 01. I'm just going to go to properties and show you this thing. 8.35 mags. It's a pretty hefty little program but you can remember everything is included. You can just double click and it just runs. You can copy this onto a USB thumb drive or email it to your friends or whatever you want to do and it'll just run. So that's how you do a static compilation. It's really not that hard. There's only a few key things you need to remember. You need to set the Qmake L flags in the config file for your compiler. That's qmake.config. You need to configure for your platform a static release, no exemptions, and then do a Ming32 make subsource. And if you ever wanted to switch back, you would just simply take it out and reconfigure for shared. Let's actually show you how to do that. So you would just go back into your command line and go configure. I hope if I could spell configure again. And then of course platform and you want win32 g++ and you would say shared instead of static and then release. And you don't put the no exemptions in there because you want things to be exempted. And you would just run this. It's the exact same process. It just has the different flag of shared instead of static. And then when you're building your programs, let me bring up QCreator again, in your project file, it cannot have this config plus equal static. I mean, you can just take that right out. Now, before I leave you, I want to show you another neat little thing. This executable is 8.5 megs. That's a pretty big executable. We want to shrink that down a little bit. So we're going to use another program to shrink that executable down. So first thing we want to do here is just make a copy of this. We're going to copy, paste. That way we've got a backup of this thing. So what we need to do now is open a command line and we're going to run a program called UPX. UPX is a software packing program and I'm going to cheat a little bit and just copy and paste. I'm going to post the links out for all this stuff. So don't worry about hitting Google and trying to find all this as you're listening. UPX, what it does is it takes your file, your executable, and compresses it down. And it does that by doing a lot of voodoo magic that, quite honestly, I don't really understand. I just know it works. There are a lot of other packers out there, but UPX is free. Free is good, right? It's got some commands here. So let's look at the usage. UPX, 1 through 9, a bunch of letters. And really what that says is how much do you want to compress? One is fast, but less compression. Nine is slow, but better compression. And then you want some extra options here. Dash O for output file and then the file you want to compress or decompress. So it's pretty simple. We're just going to do UPX dash nine for the best compression out there. Dash O and then the file we want to compress to and then the file we want to compress. So I'm just going to a little copy and paste magic here. I've already got it all written up. I've got UPX dash nine dash O. There's the file I want to output to. Notice the O1C.exe and then the file we want to compress. You notice how there's no O1C? Well, because we're going to create that. And when you hit enter, if all goes well, you'll see this little progress bar down here and you can actually see it counting away 34, 35. It's actually pretty snappy. It's actually one of the better compresses I've used out there. And it says packed ratio 36.56% win 32 PE static test O1C.exe. So let's minimize that. And sure enough, there is our other file. And you can see the original is 8.56 and the compressed is 3x. Runs the same looks the same acts the same. It's just smaller. That's the magic of UPX. So there you have it boys and girls static compilation at its finest. Like I said, I'm not an expert at this. So if you're throwing your hands up there going Brian, how do I do it for Linux? How do I do it for Mac? Quite honestly, I don't know. But what I do know is that I will post links that I found that help me out on my website. So when you go to my website, let me actually pull it up here. Void Realms.com and I'll put a link out on YouTube for it. Just go to tutorials and then cute. And you can just pretty much just go to the last page and I'll have it out here or you can just type in the word static and search and bring it up. And I'll, like I said, I'll put as much information into the description as I possibly can. Including all the links that I use to figure all this stuff out. So this is Brian. Thank you for watching. I hope you found this video educational and entertaining and keep up with the feedback. Honestly, this was a user feedback video. I would not have done this if I had not had so many of you saying, how do I do this? So thanks.