-->
Previous | Table of Contents | Next |
Working with Cc
In normal operation, the cc command executes a number of other commands under the hood. One such command is cpp. The cpp command is the C preprocessor. This reads a C program file, a .c file, and expands any # directives. In the short program you typed in earlier, the #include directive means to include the file stdio.h. That is, cpp reads in stdio.h and inserts the contents right at the #include directive. Most C programs use one or more include files.
These include files are normally stored in /usr/include. If you use the angle brackets, (<) and (>), around an include filename, like <stdio.h>, this means that cpp looks for a file named stdio.h in the standard places, of which /usr/include is the default (the -I command-line parameter can add more directories to the include file search path; see Table 10.2 later). You can also use quotation marks () around the filename.
Parameter | Meaning |
---|---|
-Idirectory | Searches the given directory and /usr/include for include files |
-c filename.c | Compiles the file filename.c and builds the object module filename.o; this does not create an executable command |
-o progname | Names the executable program progname; the default name is a.out |
-g | Compiles with debugging information |
-O | Optimizes the program for best performance |
-lLibrary | Link in the named library |
All C programs are built around the section labeled main(). The main() section (called a function in C parlance) is executed when the program starts. Our main() function simply calls the printf() function, which prints the text between the quotation marks to your screen. As you can tell, this is not a sophisticated program.
The \n character passed to printf() in our program means that a newline character is printed. This starts a new line. If youre used to a DOS machine, youll note that UNIX uses a newline character where DOS uses a carriage return and then a new line. The backslash, \, is used as a special character in C programs. Usually, a backslash is combined with another character to make a nonprintable character, such as \n for a new line, \t for a tab, or \a for a bell.
The cc command uses a number of command-line parameters to tell it what to do and to allow you to fine-tune the process of building executable programs from C language text files. Table 10.2 lists commonly used cc command-line parameters.
Most UNIX compilers dont allow you to mix the g (include debugging information) and O (optimize) options, but the GNU C compiler used by Linux allows this.
There are many cc command-line options; use man cc to see them.
Linking with Libraries
For C programs, a library is a collection of commonly used routines that you can reuse in your programs. Most C programs require more than just the standard C library. If you look in /usr/lib, youll see most of the libraries supported by Linux. Table 10.3 lists the major locations for Linux libraries.
Directory | Libraries |
---|---|
/usr/lib | Main system libraries |
/usr/openwin/lib | Open Look libraries like the Xview library |
/usr/X11R6/lib | Most X Window libraries |
To link with a given library, you use the -l command-line option to cc. To link with the X11 library, use -lX11; this is shorthand notion for linking in the library named libX11.a (or its shared-library equivalent, libX11.so).
Previous | Table of Contents | Next |