-->
Previous Table of Contents Next


Chapter 26
Programming in C

by Rick McMullin

In This Chapter
•   What is C?
•   The GNU C compiler
•   Debugging GCC programs with
•   Additional C programming tools

Linux is distributed with a wide range of software-development tools. Many of these tools support the development of C and C++ applications. This chapter describes the tools that can be used to develop and debug C applications under Linux. It is not intended to be a tutorial on the C programming language, but rather to describe how to use the C compiler and some of the other C programming tools that are included with Linux.

We will look at some of the useful C tools that are included with the Linux distribution. These tools include pretty print programs, additional debugging tools, and automatic function prototypers.


Tip:  
Pretty print programs are programs that automatically reformat code so that it has consistent indenting.

What Is C?

C is a general-purpose programming language that has been around since the early days of the UNIX operating system. It was originally created by Dennis Ritchie at Bell Laboratories to aid in the development of UNIX. The first versions of UNIX were written using assembly language and a language called B. C was developed to overcome some of the shortcomings of B. Since that time, C has become one of the most widely used computer languages in the world.

Why did C gain so much support in the programming world? Some of the reasons that C is so commonly used include the following:

  It is a very portable language. Almost any computer that you can think of has at least one C compiler available for it, and the language syntax and function libraries are standardized across platforms. This is a very attractive feature for developers.
  Executable programs written in C are fast.
  C is the system language of all versions of UNIX.

C has evolved quite a bit over the last 20 years. In the late 1980s, the American National Standards Institute published a standard for the C language known as ANSI C. This further helped to secure C’s future by making it even more consistent between platforms. The 1980s also saw an object-oriented extension to C called C++. C++ will be described in the next chapter, “Programming in C++.”

The C compiler that is available for Linux is the GNU C compiler, abbreviated GCC. This compiler was created under the Free Software Foundation’s programming license and is therefore freely distributable. You will find it on the book’s companion CD-ROM.

The GNU C Compiler

The GNU C Compiler (GCC) that is packaged with the Slackware Linux distribution is a fully functional, ANSI C–compatible compiler. If you are familiar with a C compiler on a different operating system or hardware platform, you will be able to learn GCC very quickly. This section describes how to invoke GCC and introduces many of the commonly used GCC compiler options.

Invoking GCC

The GCC compiler is invoked by passing it a number of options and a number of filenames. The basic syntax for invoking gcc is this:


gcc [options] [filenames]

The operations specified by the command-line options are performed on each of the files that are specified on the command line. The next section describes the options that you will use most often.

GCC Options

There are more than 100 compiler options that can be passed to GCC. You will probably never use many of these options, but you will use some of them on a regular basis. Many of the GCC options consist of more than one character. For this reason you must specify each option with its own hyphen, and you cannot group options after a single hyphen as you can with most Linux commands. For example, the following two commands are not the same:


gcc -p -g test.c



gcc -pg test.c

The first command tells GCC to compile test.c with profile information for the prof command and also to store debugging information within the executable. The second command simply tells GCC to compile test.c with profile information for the gprof command.

When you compile a program using gcc without any command-line options, it creates an executable file (assuming that the compile was successful) and calls it a.out. For example, the following command creates a file named a.out in the current directory:


gcc test.c

To specify a name other than a.out for the executable file, you can use the -o compiler option. For example, to compile a C program file named count.c into an executable file named count, type the following command:


gcc -o count count.c


Tip:  
When you are using the -o option, the executable filename must occur directly after the -o on the command line.

There are also compiler options that allow you to specify how far you want the compile to proceed. The -c option tells GCC to compile the code into object code and to skip the assembly and linking stages of the compile. This option is used quite often because it makes the compilation of multifile C programs faster and easier to manage. Object code files that are created by GCC have a .o extension by default.

The -S compiler option tells GCC to stop the compile after it has generated the assembler files for the C code. Assembler files that are generated by GCC have a .s extension by default. The -E option instructs the compiler to perform only the preprocessing compiler stage on the input files. When this option is used, the output from the preprocessor is sent to the standard output rather than being stored in a file.


Previous Table of Contents Next