Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Linux System Programming Techniques

You're reading from   Linux System Programming Techniques Become a proficient Linux system programmer using expert recipes and techniques

Arrow left icon
Product type Paperback
Published in May 2021
Publisher Packt
ISBN-13 9781789951288
Length 432 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Jack-Benny Persson Jack-Benny Persson
Author Profile Icon Jack-Benny Persson
Jack-Benny Persson
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Chapter 1: Getting the Necessary Tools and Writing Our First Linux Programs 2. Chapter 2: Making Your Programs Easy to Script FREE CHAPTER 3. Chapter 3: Diving Deep into C in Linux 4. Chapter 4: Handling Errors in Your Programs 5. Chapter 5: Working with File I/O and Filesystem Operations 6. Chapter 6: Spawning Processes and Using Job Control 7. Chapter 7: Using systemd to Handle Your Daemons 8. Chapter 8: Creating Shared Libraries 9. Chapter 9: Terminal I/O and Changing Terminal Behavior 10. Chapter 10: Using Different Kinds of IPC 11. Chapter 11: Using Threads in Your Programs 12. Chapter 12: Debugging Your Programs 13. Other Books You May Enjoy

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:

  1. Update the repository cache to get the latest version in the next step:
    $> sudo apt-get update
  2. Install the build-essential package, and answer y 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:

  1. 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.
  2. 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;
    }
  3. Now, compile it using GCC. This command produces a file called a.out:
    $> gcc first-example.c
  4. 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!
  5. 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 name first-example:
    $> gcc first-example.c -o first-example
  6. Let's rerun the program, this time with the new name, first-example:
    $> ./first-example
    Hello world!
  7. Now, let's try to compile it using Make instead:
    $> rm first-example
    $> make first-example
    cc     first-example.c   -o first-example
  8. 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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image