Udemy

Compiling Software from Source Code

A free video tutorial from Ziyad Yehia
Energetic Instructor with a Project-based Approach
Rating: 4.7 out of 5Instructor rating
3 courses
347,515 students
Compiling Software from Source Code

Lecture description

In this video, you will learn how to download source code from gnu.org, and then customise, compile and install it to demonstrate the freedom that free open source software gives you.

Learn more from the full course

Linux Mastery: Master the Linux Command Line in 11.5 Hours

Learn the Linux Command Line from Scratch and Improve your Career with the World's Most Fun Project-Based Linux Course!

10:35:03 of on-demand video • Updated April 2023

Quickly Learn the Linux Command Line from Scratch!
Use Bash Scripts and Cron Scheduling Software to Automate Boring Tasks!
Become an Independent User of the Linux Operating System!
Learn how to Search for, Customise, Install and Manage Open Source Software with Package Managers!
Operate a Linux Computer Entirely from the Command Line!
Gain a Complete Understanding of Linux & fill in any existing knowledge gaps!
English [CC]
Instructor: Hello there, you beautiful people and welcome to this video where we're going to be learning how to turn source code into runnable programs with our own little modifications. So in this video, you're going to learn how to download source code, edit it, and use the GNU C compiler to turn that code into runnable binary programs. You're also going to learn how to install software from source code using the make command. And we're also gonna modify the code slightly, so that we can see that it is indeed our modified code that is being installed. And by the end of the video, you should have a much better understanding of the awesomeness of open source free software, and you should also know how to compile software from source code for yourself. So let's go ahead and get right into it. All right, so here we are again on the gnu.org website and we can see on the SOFTWARE tab here, that we can get each of the packages, scrolling down to the bottom, that comes with the GNU operating system. Now all the source code for the commands that we've run so far, such as the fine command, the ls command and so on, can all be found in this coreutils package over here. So let's go ahead and download this package and take a look at it. So to do that, we click on the link, and it'll open up another page. And if we scroll down to the Downloads section here, it'll give us a couple of links, and it tells us that the stable source releases can be found at this link, and the test source releases can be found at this link. Now the difference between the stable and test is that stable source releases, you know, are known to work, they've been tested, whereas test source releases are, you know, maybe where they're trying out new things, and you know, it's not fully stable, it might, it's not final, right? So we're gonna try at the stable source and we're gonna click on that link. And we can see here that we get a bunch of packages, a bunch of bundles that are in a list format, and the most recent versions are towards the bottom of the list. So if we scroll down as far as we can to the bottom of the list, we'll see we get coreutils version 8.28. And what we're gonna click on is the one that ends in .xz. So not the one that ends in .xz.sig, just the one that ends in .xz. So if I go ahead and click on that right now, we'll see that we see a popup come here, and I'm gonna click Save File. I'm gonna click OK. And that has been saved in our Downloads folder. So if I just, if I head over to our downloads folder right now in the terminal, and you can see at the minute I'm in our home directory. I go to the Downloads folder and list it out, and you can see that we have got this coreutils version 8.28.tar.xz. Now that's a compressed tar archive that's being compressed using the xz compression algorithm. So if I look at that with the fine command, we'll see that it's xz compressed data. Now xz is just a compression algorithm just like the bzip or the gzip compression algorithms that we touched on in the file compression and archiving video very briefly, because it's not one of the more common algorithms, but we can unpack it using the tar command. Okay? So let's just do that. What we can do is we can type tar, and you know, as you know how to extract a file, you give it the x and f options, and then you tell it which archive. But to extract through the xzip compression algorithm you need to give it the capital J option. So the lowercase J option is for bzip2, but the capital J option is for xzip. And if you want more information on this, check out the file compression and archiving cheat sheet that I gave you in the appropriate video, okay? So if we go ahead and press Enter, we'll see that we didn't get any information, 'cause we didn't give the tar command the v option. But if now I use ls, we'll see that we've now got a new folder called coreutils-8.28. So if we cd into there, so we cd into coreutils, and take a look, we can see that we've got a whole bunch of stuff in there, okay? But we can specifically pay attention to this src directory. Now that src is short for source, and that's where the source code is kept. So if we cd into src, and take a look by clearing the screen, take a look, we can see that we've got a whole bunch of files in here. Let's pipe that ls data into the less command, first of all. Now this makes it a bit easier to scroll up and down and you see. Now we can see that we get loads of files that ending in .c. Now that .c indicates to us that these files are all written in the C programming language, okay? Which is a very common programming language that the GNU software is written in. Given the date of when it was written, being in, you know, the 1980s, 1990s. So let's take a look at the code behind the ls command, for example. We could ls, let's just grep out ls. We can see that we've got this ls.c file going on here. So let's take a look at it, nano ls.c. And there we are. This is actually the source code in here. This is all the source code. Let me maximize it a bit. The source code for the ls command. So isn't that awesome, right? Now if you had the time, the ability, and the intention, you could go through this code and study exactly what it was doing, which is one of the freedoms that the free software movement aims to give you. But we can actually also modify this code and recompile it into a running software, which is what we're going to do now. Now this isn't a course on C programming, but in every C program, there is a function in there called the main function. And it's a very important function that always runs, okay? Now I'm going to modify that function so that whenever ls runs, the computer will print out "Hello there you beautiful people," onto the screen. (laughs) Okay? So I'm going to type a bit of code into the main function that makes that happen. Now, after preparing a bit for this lecture, I found that the main function in this file starts at about line 1,443. So if we use nano's go to line function using the Control + Underscore option, if I put in line 1,443 and press Enter, you can see that we've ended up at the main function, magic. So I'm gonna add a line of code right at the top here that does that printing stuff. So printf, and I'm gonna put in there, "Hello there you beautiful people," and make sure that it ends on a new line with the \n character right there, okay? And I'm just gonna tab this over. It doesn't make a difference, but you know, there, that's a bit nicer. And let's try and get rid of this extra space at the end of the line. It doesn't matter, but okay, there we are. So we've put in the function there, "Hello there you beautiful people," with a new line character, okay? So you know, it goes to a new line. Now if we save the file with Control + O, and we exit with Control + X, we've now modified the ls command's code. Okay? So that's pretty cool. But now how do we turn this code into a runnable program and install it on our computer? Well, because they're written in C, the code needs to be compiled into machine language before it can be run. And for that, we need a compiler and more specifically a compiler for the C programming language. Now the GNU C compiler, or GCC, is the compiler that's used on GNU Linux systems, and we can get it by entering the following command. And that following command is sudo apt-get install gcc. Now, for now, I don't want you to worry too much about this because we'll be explaining in great detail later on what this command actually means and we'll be doing it in this section of the course. But in short, what we're doing is we're telling the computer to look on the software repositories available for Ubuntu, and download and install the GNU C compiler or GCC package. Again, this will become much clearer in the next few videos but just type it as I have done so here, and when you're happy, press Enter. And because we've used sudo, it'll ask us for our password, just like when we've used sudo before. So I'm just gonna enter that in there, hopefully correctly, fingers crossed. Nope, let's try it again. There we go. And now it's saying "Hey, we've got to install a whole bunch of packages in order to get this GCC package, and it's gonna be about 72 megabytes of additional disk space. Do you want to continue?" Now I can just type yes. So I'm just gonna type Y, press Enter, and it's gonna go ahead and do that, okay? And this might take a bit of while depending on your internet connection, but it appears to have downloaded it in my case. And now it's just doing the installation. It's gonna start unpacking each of those different packages that it's downloaded, and we are actually seeing an installation process happen right now. We will look into this a lot more in more detail later on in the course, but you can see that we didn't have to download any wizard and click next, next, next, next, next, agree to any terms of conditions or anything like that, because all of this software is under the GNU public license and it all gives you the same freedoms as each other. Okay? So now that's gone ahead and installed. We didn't have to do anything. We've now installed the GNU C compiler and a whole bunch of other stuff. So now we're ready to compile our code, okay? So let's turn our attention to our beautiful source code. Now, because different computers have different architectures, we need to configure the installation of this code to our specific machine. Now there's a script that comes with that called configure. Now we're in the source folder here. So let's just go back up a folder with cd.. to go back up folder, clear the screen and take another look. And you can see here there's a script called configure. And we know it's a script because it's green, okay? So let's run that by typing bash configure, and we press Enter. And what this is doing is, it's configuring the GNU C compiler, the GCC, to make sure that when it compiles all this, the software that we tell it to, it's gonna do it in a way that's appropriate for our computer's architecture. Okay? And it's making the appropriate adjustments. And the important thing that this configure script does, besides configuring the GNU C compiler, is that it creates a new file called the make file. Now the make file is responsible for the installation of this new software package, but to make the make file work, which is a massive tongue twister by the way, we need a new command called make. So when this thing is finished configuring, we're going to install the make command in a very similar way to the way we just installed the GNU C compiler. So if we just sit tight here for a second while we see all this beautiful text flying up the screen. (laughs) It looks pretty awesome though, right? It's like something you might see on like a film with hackers, you know, and stuff like that. And we're literally doing that right now, so be very proud of yourself. Okay? So now that's finished. If we just take a look in this directory, we should be able to see this new file here called the make file. Now the make file is an important file, but to be able to run that, we need to have the make command. So to get that we're gonna type, sudo apt-get install make, okay? And when we press Enter, it's gonna go through a very similar process of installation. And now that's all installed, very quick, very easy to install that one, okay? So now when we're in the same folder as the make file, if we just run the make command, you can see that it's gonna go through and it's gonna compile all of the C files that it finds have not been compiled recently. And there are still some changes outstanding, which is effectively all of the C code in this folder including our ls file, okay? So what this is doing is it's compiling it all into machine code, into binary code that can be ran on the computer, okay? So this is what's necessary when you run a compiled language like the C programming language. So when that's done, we'll be able to install the newly compiled programs. But I'm gonna cut the video here to save time, and you know, stop you waiting around too much. Okay, now that's completed. So what's happened is we found the code for the ls command, okay? We edited that code, and then we installed something called the GNU C compiler, which is what's required to turn that code into machine code that the computer needs in order to run, okay? So what we did is we installed the GNU C compiler, and we had to configure that compiler before it could work correctly for our computer's specific requirements, okay? So we configured the compiler and then when it was configured, we've just run it using the make command, okay? To make sure that every single bit of code is now turned into machine code, okay? So the only step that remains, now that we've got this machine code, is to install that machine code in the required places on our path for it to actually work. And the way that we can do that is using the sudo make install command. So if I clear the screen, type sudo make install, what it's doing is it's installing all of the stuff like that, and that was very, very quick. We've now installed each of the different pieces of software that came with this version 8.28 of the coreutils package, okay? So let's take a look at how that works, okay? So if we just close our terminal and reopen it again, then now if we run the ls command, we can see that it says, "Hello there you beautiful people," every time that it runs, because we modified our code and installed it. How awesome is that, right? So hooray for open source and free software. So this is actually a much faster process in reality than what I've showed you here. So to change back, we would just re-edit the source code, recompile with make, then re-install with make install. So if I head back to our Downloads, okay? It's actually a much faster process when you get a bit better with it. So here we are, we are in our coreutils file, we're gonna go over to our source code folder. Now we're in there, okay? So let's go to nano ls.c. Let's head over to line 1,443, and we're gonna delete this line that I put in there. Save the file, okay? Now if I just clear the screen, now what we have to do is do make && sudo make install. Oh, 'cause I'm in the wrong folder, there we go, make && sudo make install, okay? So now notice how the make command is just really compiling the ls command, 'cause it notices that's what's different. Let me run with our, enter my password there, so it can install it. And now when I close the terminal and reopen it, and use the ls command, now it's back to normal. So you see how much faster that was, right? Once we've configured the GNU C compiler once, or at least for the package, it's really fast to just go and edit it thereafter, okay? That's because the make command knew as well that it only needed to recompile the code that was affected by our change, not the whole package, which was new to the system the first time we did it, okay? So make is a very clever program. And as I say, now that we've installed the new version of the ls program, now the "Hello you beautiful people" message has disappeared and it's back the way that it was. So there you are, you beautiful people. In this video, you've seen how to access source code from the gnu.org website, how to modify that source code, and how to use, configure, make, and make install, to install the new software. Now the great thing is you can access and modify the source code for everything on a GNU Linux operating system. And if you care to exercise it, you have absolute power over how your computer works. So hopefully, you can see how having open source free software running on your computer is a great asset, and you also, that you can appreciate the effort that the pioneers of such a system took to build it. But the process that we went through in this video is relatively lengthy, and sometimes you just want to install software and get on with what you were doing. So for this, there are massive bodies of maintained and pre-compiled code in what are known as the software repositories. And we used the repositories in this section actually to install the GNU C compiler and the make command. And software repositories are what you'll use to install software 99% of the time. And they come with so many really cool features. So now that you understand that, yes, you can look up source code and modify how your computer works, however you like, manually, let's take a look at how the software repositories, which give you access to a massive library of software that is already pre-compiled and ready to go. Let's have a look at how they work as well. So for all that goodness, I'll see you in the next video.
Powered by Seotoolzbundle.com