Hello, and welcome to this demonstration, a demonstration where you're
going to learn a little bit about working with source code.
So on our Ubuntu system,
we're going to install a mapper port scanner from source code, as well as
creating our own very simple source file so we can get an idea of the processes
behind compiling and creating binaries from C files.
Now working on our Ubuntu system,
I've already gone through and installed the software, but I
don't want you to miss out. If I go through and just take a look
at the install.sh file, this is a shell script that goes through
and runs the installation.
So I've already executed this.
We don't have to wait for the software installing, and you'll be
able to go through and download this script if you want to save a
little bit of typing from the course files.
We're going to go through now and run our git clone, so
this is copying a repository locally.
We're going to go through and take a look at our https
and pointing through to github.com.
And from here, we can go through and say,
well, we want to work with the Nmap repositories, and Nmap
might have multiple projects to download.
So Nmap really is the author.
We want to look at the Nmap Git file.
I can go nmap or nmap.git.
It doesn't really matter. As I go through, we will download then
the source code, and it's going to create a directory, as we can
see, called nmap, Cloning into 'nmap'. Now we'll be downloading
the latest version.
We could perhaps go through and look at different repositories
to download that have different versions.
But by default, we're going to be picking up on the latest version.
Now if I move into that Nmap directory, I didn't need to create it.
It was created by Git.
And when I ls here, then we've got all of the bits and
pieces, if you like, to get started.
If I go through and do an ls and then mak*,
this moves us up to the top, we see we don't appear to have
everything, but remember, we're case sensitive.
So if I go to look for the uppercase M, then we've got a Makefile and .in.
This then becomes the template that is used to build our Makefile.
The Makefile will have a list of instructions. To create the
Makefile, we go through and run the configure script.
The configure script is going to run through and make sure
that we have a compiler and we have the libraries that are
necessary to compile our code.
It will also then create from the template a customized
Makefile that will work with our environment.
So I can go through and run the configure. And as you can see, a lot of
easy or preflight checks. If we're missing a library,
it will tell us we're missing a library, and of course, we need
the source code for that library. But there you go, we are ready
to go. If I go back now and ls,
we can see that we've still got the template file for the Makefile, but we now
have the Makefile, and that has our instructions. So I can then go through and
just run make. Make without any arguments is going to run through the compile.
This might take a little while, so we can leave this running. Now once that has
finished its compilation, and it really didn't take that long, maybe it took 3
or 4 minutes, I'm going to clear my screen.
Everything then is being compiled, but it's all within our project directory.
We're going to go through now and use a sudo and make install, and with
this, it should run relatively quickly, as we can see, where we then added
in the binaries to their correct locations.
If I go through and clear the screen and now go through and run my
nmap ‑V, we can see then that I've got version 7.9.
If I go through and check the version that is available, so if I go
through, and we'll do an apt search, and we'll look for the regular
expression just for Nmap, we can see that this is 7.8.
So we have got the later version.
Okay, well, that's done that while we've worked with somebody else's code.
But moving back into our home directory,
I bet you guys want to be able to create your own C
program from your own fingers.
Come on, let's go through and take a look. If I go
through and vim, and we'll just call it,
let's say, hello.c, .c for a C source file, although it doesn't really
matter. Now we can go through then and hit our insert key, and we're
going to put what might appear to be a comment.
But remember,
this is a pre‑processor command we're saying that we're going to include,
and these are then library or header files. Where we put them inside of our
diamond brackets, they're going to come from the normal search path rather
than from the path that we type.
So we're going to put our stdio.h.
This is going to allow me to read in and write out
information. We're then using void.
This is going to be from the output of a function that we're going to call main.
Now we're saying that we're not receiving anything from this function, but we
could be receiving things like error codes such as an integer or other
functions might receive character data. We will go through and say we don't
need to pass any parameters through to the main function, and we go in and
define then with our brace brackets, the code.
All we're going to do is write to the screen, so printf. We
open up then the parentheses for the function and write our
hello or whatever we want.
I'm going to put a new line after this and close off our parentheses,
and we have a semicolon at the end of a line of code.
We're now finished with this function.
I can use Ctrl+D to return my cursor to the beginning and
then use the closing brace bracket.
We can see then that my cursor is currently on the closing brace bracket.
If I hit escape, if I need to go up to the opening brace bracket,
I can just use the % key, and we can see now that my cursor has moved to
the top, % again, my cursor has moved to the bottom. So a lot of people
think that you do need a full‑on IDE, but there's a lot of really cool
features for programmers built into vim.
So this is just one of the very tiny features where we can toggle between
our opening and closing brace brackets using the % key.
Let's go through and jump out of this, and then we can go
through and use our compiler, and we can then specify that we
want to compile our hello.c file, and we can compile it to our
program that we'll call hello.
With that done,
I can go through then and run my hello program, and we print hello to
the screen. Remarkable, I know, but at least we've created and understood a little bit more about source code.