-->
Previous | Table of Contents | Next |
The books ends with Chapter 10, an overview of Linux programming. In it youll find explanations of Linuxs programming tools (including the GNU C compiler) and its X Window programming tools.
This chapter covers:
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. Well cover a lot of the odd things that youre 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 youd 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, weve thrown freeware called LessTif on the second accompanying CD-ROM, for those of you who want Motif compatibility but dont want to pay for commercial software.
Your main worry is whether youve installed the proper disksets for your compiler and associated tools. (If you havent 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 havent, you can always go back and use the setup program to reinstall the proper disk sets.
If youre not a programmer, chances are that youll be lost in much of this chapter. Even so, youll find some interesting Linux utilities mentioned here. In addition, many free Linux programs come in source code-form only; youll need to learn to compile them, so its important to know about the process of compiling and linking C programs.
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 youd expect. The command-line parameters are all standard cc parameters in addition to the traditional gcc parameters. If youre used to programming on UNIX, youll find Linux works as youd expect.
For those new to C programming, well provide a short introduction. If youre really new at this, youll likely want to get a C programming book to help you out. Appendix A lists a few.
C programsand in fact, most programs in generalusually start in plain old text files. (Linux makes extensive use of simple text files, as youve 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 youre dealing with. Table 10.1 lists the most common Linux file types and their common file extensions.
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 |