Installing GCC and GNU Make
In this section, we will install the essential tools that we'll need throughout this book; namely, GCC, the compiler. It's the compiler that turns the C source code into a binary program that we can run on the system. All the C code that we write will need to be compiled.
We'll also install GNU Make, a tool that we'll be using later on to automate how projects containing more than one source file are compiled.
Getting ready
Since we are installing software on the system, we'll need to be using either the root user or a user with sudo
privileges. I will be using sudo
in this recipe, but if you are on a system without sudo
, you can switch to the root user with su
before entering the commands (and then leave out sudo
).
How to do it…
We will be installing what is called a meta-package or a group, a package that contains a collection of other packages. This meta-package includes both GCC, GNU Make, several manual pages, and other programs and libraries, which are nice to have when we're developing.
Debian-based systems
These steps work for all Debian-based systems, such as Debian, Ubuntu, and Linux Mint:
- Update the repository cache to get the latest version in the next step:
$> sudo apt-get update
- Install the
build-essential
package, and answery
when prompted:$> sudo apt-get install build-essential
Fedora-based systems
This works for all Fedora-based systems, such as Fedora, CentOS, and Red Hat:
- Install a software group called Development Tools:
$> sudo dnf group install 'Development Tools'
Verify the installation (both Debian and Fedora)
These steps are the same for both Debian and Fedora:
- Verify the installation by listing the versions installed. Note that the exact versions will differ from system to system; this is normal:
$> gcc --version gcc (Debian 8.3.0-6) 8.3.0 Copyright (C) 2018 Free Software Foundation, Inc. This is free software; see the source for copying conditions.  There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $> make --version GNU Make 4.2.1 Built for x86_64-pc-linux-gnu Copyright (C) 1988-2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
- Now, it's time to try out the GCC compiler by compiling a minimal C program. Please type the source code into an editor and save it as
first-example.c
. The program will print the text "Hello, world!" on the Terminal:#include <stdio.h> int main(void) { Â Â Â Â printf("Hello, world!\n"); Â Â Â Â return 0; }
- Now, compile it using GCC. This command produces a file called
a.out
:$> gcc first-example.c
- Now, let's try to run the program. To run a program in Linux that isn't in the usual directories for binaries (
/bin
,/sbin
,/usr/bin,
and so on), you need to type the special./
sequence before the filename. This executes the program from the current path:$> ./a.out Hello, world!
- Now, recompile the program. This time, we will specify a name for the program with the
-o
option (-o
for output). This time, the program file will have the namefirst-example
:$> gcc first-example.c -o first-example
- Let's rerun the program, this time with the new name,
first-example
:$> ./first-example Hello world!
- Now, let's try to compile it using Make instead:
$> rm first-example $> make first-example cc     first-example.c   -o first-example
- Finally, rerun the program:
$> ./first-example Hello, world!
How it works…
Installing software on the system always requires root privileges, either via a regular root user or via sudo
. Ubuntu, for example, uses sudo
and has the regular root user disabled. Debian, on the other hand, doesn't use sudo
at all in the default installation. To use it, you have to set it up yourself.
Debian and Ubuntu use the apt package manager to install software on the system. To get the latest version that is available in the repository, you need to update the cache. That's why we ran the apt-get update
command before installing the packages.
Fedora-based systems use the Red Hat Package Manager (RPM) system to install the software. The program we use to install the package is dnf
on newer versions. If you are using an older version, you might need to replace dnf
with yum
.
In both cases, we installed a group of packages that contain the utilities, manual pages, and compilers that we'll need throughout this book.
After the installation was complete, before trying to compile anything, we listed the GCC version and Make version.
Finally, we compiled a straightforward C program, first using GCC directly and then using Make. The first example with GCC produced a program with the name a.out
, which stands for assembler output. That name has a long history and goes back to the first edition of Unix in 1971. Even though the file format, a.out
, isn't used anymore, the name still lives on today.
Then, we specified a program name with the -o
option, where -o
stands for output. This produces a program with a name of our choosing. We gave the program the name first-example
.
When we used Make, we didn't need to type in the filename of the source code. We only wrote the name we wanted for the binary program produced by the compiler. The Make program is smart enough to figure out that the source code has the same filename but that it ends with .c
.
When we executed the program, we ran it as ./first-example
. The ./
sequence tells the shell that we want to run the program from the current directory. If we leave out ./
, it won't execute. By default, the shell only executes programs that are in the $PATH
variable—usually /bin
, /usr/bin
, /sbin
, and /usr/sbin
.