Compiling Software from Source Code

Ziyad Yehia
A free video tutorial from Ziyad Yehia
Energetic Udemy Instructor with a Project-based Approach
4.7 instructor rating • 2 courses • 173,484 students

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!

11:16:52 of on-demand video • Updated July 2020

  • 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 [Auto] Hello 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 editor and use the Ganu C-compiler to turn that code into runnable binary programs. You also got to learn how to install software from source code using the make command. I'm also going to modify the code slightly so that we can see that it is indeed our modified code that is being installed by the end of the video you should have a much better understanding of the awesome this 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 new dot org Web site and we can see on the software tab here that we can get each of the packages going down to the bottom that comes with the new operating system. Now all the source code for the commands that we've run so far such as the find command the ls command and so on cannot be found in this core utils package of 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 download 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 sorceresses I mean you know maybe where they're trying new things and you know it's it's not 47 it might it's not final. Right so we're going to try it at the stable source I'm going to click on that link and we can see here that we get a bunch of packages a bunch of bundles that are 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 will see we get call utils version 8 point to 8. And what we're going to click on is the one that ends in Dot X zeds and on the ends it Dot exec dot sig just the one that ends in Dot X Zed. So if I go ahead and click on that right now we'll see that we see a pop come here and I'm going to save file. I'm going to click OK. And that has been saved in our downloads folder so if I just head over to our downloads folder right now in the terminal you can see it the minute I'm in a home directory. I go to the downloads folder and list out and you can see that we have got this core utils version eight point 8.2 a 8. Todd X Zed. Now that's a compressed tar archive that's been compressed using the X Zad compression algorithms if I look at that. We'll see what the find command will say that it's ex-head compressed data. Now ex-editor just a compression algorithm just like the BS app or the GS EP compression RPMs that we touched on in the fall compression archiving video. Very briefly because it's not one of the more common algorithms but we can unpack it using the tar command. OK so let's just do that. Well we can do is we can type Taar And you know as you know harder to extract a file you give it the x and f options and then you tell it which archive to extract through the zip compression algorithm to give it the capital J option so the lowercase j option is Phoebes it to put the capital J option is for exit 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. OK. So if we go ahead and press ENTER we'll see that we didn't get any information because we didn't give the talk about the Wii option. But if now I use less we'll see. We've now got a new folder called Call utils eight point to eight. So if we see the Internet CD into core utils and take a look see that we've got a whole bunch of stuff in there. OK but we can specifically pay attention to this. S r c directory now that SIRC is short for source. And that's where the source code is capice if we CD into S or C and take a look in the screen take a look we can see that we've got a whole bunch of files in here let's pipe that data into the last command. First of all now that makes it easier to scroll up and down and you see now we can see that we get loads of files that end in dot. See now that dot Dotsie indicates to us that these files are all written in the C programming language. OK which is a very common programming language. The good news software is written in. Given the date of when it was written being in the know the the 1980s 1990s. So let's take a look at the code behind the ls command. For example we could last Nestor's. LS We can see that we've got this LS Dotsie file going on here. So let's take a look at it. Nano LS dot see. And there we are. This is actually the source code in here. This is all the source code of the Maximizer of it the source code for the LS commands. 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 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 was a function in there called the main function. It's a very important function that always runs. Ok now I'm going to modify that function so that whenever class runs the computer will print out. Hello you beautiful people onto the screen. OK so I 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 1443. So if we use Nanos go to line function using the control and underscore option if I put in line 1 4 4 3 and press ENTER you can see that we've ended up at the main function magic. So the line code right at the top here that does that printing stuff so print F and I'm going to put in there. Hello there you beautiful people and make sure that it ends on a new line with the backslash and character right there. OK. And I'm just going to tap this. It doesn't make a difference but you know that that's a bit nicer. Try and get rid of this extra space at the end the line it doesn't matter. OK so we've put in the function there. Hello there beautiful people with a new line character. OK so you know the line it goes to a new line. Now if we save the fire with control in 0 and we exit with control of X we've now modified the LS commands code. OK 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 most specifically a compiler for the C programming language. Now the new C compiler or GCC is the compiler that's used on Ganu Linux systems and we can get it by answering the following command and not following commands sudo apt get install GCC. Now for now I don't want to worry too much about this because we'll be explaining in great detail later on what this command actually means and will be doing it in this section of course but in short what we're doing is we're telling the computer to look on the software repositories available for a buntu and download and install the new C compiler or GCC package. Again this will become much clearer in the next few videos but as type it as I have done so here and when you're happy press enter. And because we've used sudo it asks for a password just like when we've used sudo before so I'm just going to add to that in there hopefully correctly. Fingers crossed. 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 that's going to be about 72 megabytes of additional disk space. Do you want to continue. Now I can just type yes. I'm going to type Y. Press enter and it's going to go ahead and do that. OK and this might take a 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 going to start unpacking each of those different packages that it's download. And we are actually seeing an installation process happen right now. We will look into this a lot more in more detail later in the course but you can see that where 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 the software is under the new Public License and it all gives you the same same different the same freedoms as each other. OK so now that's going to happen and still we don't have to do anything. We've now installed a new C compiler and a whole bunch of other stuff so we're ready to compile our code. OK so let's turn our attention to our beautiful source code. No because different computers have different architectures. We had to configure the installation of this code to our specific machine. Now there's a script that comes with that call to configure the source in the source folder has a backup of all the with CD dot dot to go back up a folder. Play the screen and take another look and we can see here there's a script called configure and we know it's a script because it's green. OK let's run that by typing bash configure and we press enter. What this is doing is it's configuring the new C compiler the GCC to get make sure that when it compiles all this the software that we tell it to it's going to do it in a way that's appropriate for our computers architecture is making the appropriate adjustments and the important thing that this configure script does besides configuring the Ghannouchi C-compiler is that it creates a new file called the Makefile and makefile is responsible for the installation of this new software package. But to make the makefile work which is a massive tongue twister by the way we need a new command called make. So when this thing has finished configuring we're going to install the make command in a very similar way to the we just installed the new C compiler. So if we just sit tight here for a second while we see all this beautiful text flying up the screen looks pretty awesome right. It's like something you might see unlike on like a film with hackers you know and stuff like that and it's we're literally doing that right now. So I'd be very proud of OK 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 fun of the make file is an important file but we need to be able to run that we need to have the make command. So to get that we're going to type sudo apt get install make. OK. And when we press Enter it's going to go through a very similar process of installation. Now that's all installed very quick very easy to install that one. OK 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 going to go through and it's going to compile all of the 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 LS file. Ok so what this is doing is it's compiling it all into into machine code into binary code that can be run on the computer. OK. 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 going to cut the video here to save time and you know stop you waiting around too much. OK. Now that's completed. So what's happened is what we found the code for the ls command. OK we edited that code and then we installed something called the new C compiler which is what's required to turn that code into machine code that the computer needs in order to run. OK. So what we did is we installed the Ghannouchi compiler and we had to configure that compiler before it would could work correctly for our computers specific requirements. OK so we configured the compiler and then when it was configured we'd just run it using the make command. OK. To make sure that every single bit of code is now turned into machine code. OK. 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 pseudo sudo make install. OK come on if I see the screen to suit or make install what it's doing is it's installing all of the stuff like that. That was very very quick. We've now installed each of the different pieces of software that came with this version 8.2 out of the core utils package. OK. So let's take a look at how that works. OK 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 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 shown you here. So to change back we would just rewrite the source code recompile with make then re-install with make install. So if I head back to our downloads OK it's actually a much faster process when you get a bit better with it. So here we are we're in our call you far. We're going to go over to our source code for the library in there. OK. So let's go to Nonno LS Dotsie. Let's head over to line 1443. I'm going to delete this line that I put in there. Save the file. OK. Now if I could clear the screen now will we have to do is do make sudo make install. Oh because I'm not I'm in the wrong folder. There we go make and sudo make install so now notice how the make command is just really compiling the ls command because it notices that what's different. I we run with our enter my password there so it can install it and then 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 C-compiler once or at least for free for the package it's really fast to just go and edit edit it thereafter. OK. That's because the make command knew as well that it only needed to recompile the code that was affected by the change not the whole package which was new to the system the first time we did it. So make is a very clever program. And as I say now that we've installed the new version of ELAS program now hello the 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 new the old web site had 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 new 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. You also 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 is known as the software repositories and we use the repositories in this section actually to install the Ganu C-compiler and the make command and software repositories are what you'll use to install software. 99 percent 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 to 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 Priore pre-compiled and ready to go. Some will look at how they work as well. So for all that goodness I'll see you in the next video.