Compiling Software from Source Code

Ziyad Yehia
A free video tutorial from Ziyad Yehia
Energetic Instructor with a Project-based Approach
4.7 instructor rating • 3 courses • 207,261 students

Lecture description

In this video, you will learn how to download source code from, 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 August 2021

  • 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 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 editor and use the Ganu 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 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 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 new 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 new operating system. Now, all the source code for the commands that we've run so far, such as the fine command, the command and so on, can all be found in this core utils 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 will 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 are known to work. They've been tested, whereas tests or releases of, you know, maybe where they're trying out new things and, you know, it's not fully safe. It might it's not final. Right. So we're going to try 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, we'll see. We get core utils version eight point to eight. And what we're going to click on is the one that ends in dot, X, Z, so not the one that ends in Z dot sig, just the one that ends in Dot, X, Z. So if I go ahead and click on that right now, we'll see that we see a pop up. Come here and I'm going to save file, I'm going to make 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 analyst out and you can see that we have got this core utils version, eight point two eight, Dr. X Z. Now that's a compressed Tor archive that's been compressed using the X Z compression algorithm. So if I look at that, we'll see with the fine, we'll see that it's X, Z compress data. Now, Z is just a compression algorithm, just like the bicep or the grip compression algorithms that we touched on in the file compression archiving video very briefly, because it's not one of the more common algorithms, but we can unpack it using the top command. OK, so let's just do that. What we can do is we can type Tor and you know, as you know, how to 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 exit a compression algorithm, you need to give it the capital J option. So the lowercase J option is for B zip too, but the capital J option is for X zip. 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 V option. But if no, I use URLs, we'll see that we've now got a new folder called Call Utils eight point to eight. So if we enter so to call utils and take a look, we can 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 kept. So if we QD into SIRC 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 data into the last 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 Indian dot see, now that Dot C indicates to us that these files are all written in the C programming language, OK, which is a very common programming language that the new software is written in, given the date of when it was written being in the 1980s and 1990s. So let's take a look at the code behind the last command, for example, we could just grapplers we can see that we've got this dot C file going on here. So let's take a look at it. Nanomoles, dot 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 URLs 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. OK, now I'm going to modify that function so that whenever else runs, the computer will print out. Hello there, you beautiful people onto the screen. OK, 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 1443. So if we use Nanos, go to line function using the control and underscore option. If I put in line one, four, four, three and press enter, you can see that we've ended up at the main function magic. So I'm going to add a line of code right at the top 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 top this so it doesn't make a difference. But you know that that's a bit nicer. And let's try and get rid of this extra space at the end the line. It doesn't matter, but OK, there we are. So we've put in the function there. Hello there, you beautiful people with a new line character. You know the line, it goes to a new line. Now, if we save the file with control and oh, and we exit with control of X, we've now modified the commands code. OK, 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 new C compiler or Jinksy is the compiler that's used on Ganu Linux Systems and we can get it by entering the following command and that following command is pseudo apte get install JTC. 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 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 new C compiler or 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 use pseudo, it'll ask us for our password, just like when we've used sudo before. So I'm just going to enter 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 package and it's going to be about 72 megabytes of additional disk space. Do you want to continue now? I can just type yes. I'm just going to type Y press enter and it's going to go ahead and do that. Okay. And this might take a bit of 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 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 new public license and it all gives you the same same different the same freedoms as as each other. OK, so now that's gone ahead and installed. We didn't have to do anything. We've now installed the Ganassi compiler and a whole bunch of other stuff. So now we're ready to compile a code. OK, 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. We're in the source folder here. So let's just go back up a folder with CD Dot Dot to go back up a folder, play 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 Basche configure and we press enter. And what this is doing is it's configuring the Ganu C compiler, the jack 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 Ganassi compiler, is that it creates a new file called the Makefile. Now, the 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 is finished configuring, we're going to install the make command in a very similar way to that. We just installed the GANU. C compiler. So if we just sit tight here for a second while we see all this beautiful text flying up the screen, it looks pretty awesome now, right? It's like something you might see, unlike unlike a film with hackers, you know, and stuff like that. This we're literally doing that right now. So so be very proud of yourself. 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 Makefile. Now, the makefile 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 pseudo Atget, 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 Makefile, 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 Arielle's 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. So this is what's necessary when you run a compiled language like these 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 we found the code for the command, OK, we edited that code and then we installed something called the Ganu 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 Ganassi 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've just run it using the make a command 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 pseudo make install commands. If I clear the screen to 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, eight point to eight 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 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 hurray 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 rewrite the source code, recompile with make, then reinstall with make install. So if I head back to our downloads, it's actually a much faster process when you get a bit better with it. So here we are. We're in our core utils file. We're going to go over to our source code folder. Now we're in there. OK, so let's go to Nanomoles Dot, see, let's head over to line 1443 and we're going to delete this this line that I put in there. Save the file. OK, now, if I just clear the screen now, will we have to do is do make and sudo make install, OK? Because I'm not I'm in the wrong folder. There we go. Make and sudo make install. OK, so now notice how the make come on is just really compiling the one because it notices that's what's different. Let me run with our land to my password there so it can install it. And then when I close the terminal and reopen it and use the command, now it's back to normal. So you see how much faster that was. Right. Once we've configured the compiler once or at least for if we for the package, it's really faster. Just go and edit thereafter. That's because the make a menu as well that it only needed to recompile the code that was affected by a change, not the whole package which was new to the system the first time we did it. OK, so MAKE is a very clever program. And as I say, now that we've installed the new version of this 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 new dot org website, 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. 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 use the repositories in this section actually to install the Ganu C compiler and the meek 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 at 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 and have a look at how they work as well. So for all that goodness, I'll see you in the next video.