-->
Previous Table of Contents Next


Section IV
Linux Programming

The books ends with Chapter 10, an overview of Linux programming. In it you’ll find explanations of Linux’s programming tools (including the GNU C compiler) and its X Window programming tools.

Chapter 10
Programming in Linux

This chapter covers:

  The GNU C compiler
  C programming
  The cc command
  Using make
  Programming under the X Window System
  Using LessTif to mimic Motif
  Using shared libraries
  Using imake
  Using Tcl/Tk
  A short introduction to using Perl
  Using gawk, the GNU Project version of awk

Programming under Linux

This chapter is not going to turn you into an instant Linux and X Window programmer. We will, however, show you how to program in the Linux environment. We’ll cover a lot of the odd things that you’re supposed to know when programming on Linux, including where the X libraries are and some interesting tidbits about how Linux uses shared libraries.

For the programmer, Linux offers all the freeware utilities and compilers you’d expect for software that relies heavily on offerings from the Free Software Foundation. Starting with the GNU C compiler, you can develop C, C++, Fortran (via g77), and Objective-C programs on your Linux system. In addition to these mainstream languages, Linux supports Tcl, Perl, and a host of other programming languages and second utilities. In addition, we’ve thrown freeware called LessTif on the second accompanying CD-ROM, for those of you who want Motif compatibility but don’t want to pay for commercial software.

Your main worry is whether you’ve installed the proper disksets for your compiler and associated tools. (If you haven’t heard of one before, a compiler is a tool that converts a program in text form into an executable Linux command.) Being programmers ourselves, we always recommend this; if you haven’t, you can always go back and use the setup program to reinstall the proper disk sets.

If you’re not a programmer, chances are that you’ll be lost in much of this chapter. Even so, you’ll find some interesting Linux utilities mentioned here. In addition, many free Linux programs come in source code-form only; you’ll need to learn to compile them, so it’s important to know about the process of compiling and linking C programs.

The Linux C Compiler: GNU CC

The main C and C++ compiler on Linux is the GNU gcc. It is an all-encompassing program and can compile a number of programming languages: C, C++, Fortran, and Objective-C. Gcc, or cc, which is linked to gcc, compiles C and C++ programs just like you’d expect. The command-line parameters are all standard cc parameters in addition to the traditional gcc parameters. If you’re used to programming on UNIX, you’ll find Linux works as you’d expect.

For those new to C programming, we’ll provide a short introduction. If you’re really new at this, you’ll likely want to get a C programming book to help you out. Appendix A lists a few.

C Programming

C programs—and in fact, most programs in general—usually start in plain old text files. (Linux makes extensive use of simple text files, as you’ve seen throughout this book.) These text files are created with text editors like vi or emacs. Once created, C programs must be compiled with a C compiler, cc or gcc (which are one and the same on Linux). This C compiler converts the text file, which the programmer wrote, into object, or machine, code for the Intel platform. Then, object modules (files of object code) are linked together to make an executable program, a brand new Linux command. Once the process is successfully completed, you can execute this program like any other command you type at the command line. Being able to create your own command is a neat thing.

In addition to creating C or C++ programs, you can use shell scripts or write code in a number of interpreted languages including Perl and Tcl, which we cover later in this chapter. From the plethora of Linux program-creation tools, you need to choose the appropriate tool for any given task.

The first step is identifying what types of files you’re dealing with. Table 10.1 lists the most common Linux file types and their common file extensions.

Table 10.1 Program File Types
File Suffix Meaning

.a Library
.c C program
.C C++ file (note the uppercase C)
.cc C++ file
.cpp C++ file
.cxx C++ file
.c++ C++ file
.f Fortran program
.for Fortran program
.h C or C++ include file
.hxx C++ include file
.o Object module (compiled from a .c file)
.pl Perl script
.pm Perl module script
.s Assembly code
.sa Shared library stubs linked with your program
.so.n Run-time shared library, version number is n
.tcl Tcl script
.tk Tcl script

Most C programs are stored in one or more files that end with .c, for example, as neatstuff.c and myprog.c. When you compile a C file, the C compiler, cc, creates an object file, usually ending with .o. The linker (called linkage editor in Linux parlance), ld, then links the .o files to make an executable program. The default name for this program is a.out, although no one really uses a.out for their program names. Instead, programs have names like ls, cp, or mv. All of this is controlled by the cc command.


Previous Table of Contents Next