-->
Previous | Table of Contents | Next |
The cc command executes the C compiler, which can compile and link C programs into executable commands. To test your Linux C compiler, well use the following short program:
/* * Example C program for Chapter 10, * Linux Configuration and Installation. */ #include <stdio.h> int main(int argc, char** argv) { /* This is a comment. */ printf("Linux is my favorite O.S.\n"); return 0; } /* chap10.c */
Enter the preceding code into a text file named chap10.c, using your favorite Linux text editor.
Its a good idea to always name C program files with a .c extension. This isnt required, but following conventions like this makes Linux easier to use.
After you type in this short program, you can do the following simple steps to create a working executable program from this C file.
The program you typed in was simply a text file. Theres nothing in it to make it an executable command. To do so, we need to compile and link the program. Both steps are accomplished by the following cc command:
$ cc -o chap10 chap10.c
This command runs the C compiler, cc. The -o option tells cc to build a program named chap10 (the default name without the -o option is the awkward a.out). The chap10.c part of the command tells cc to compile the file named chap10.c. The cc command both compiled and linked the program.
You should now have an executable program named chap10. You can execute this program by typing chap10 at the command line. When you do, youll see the following output:
$ chap10 Linux is my favorite O.S.
Now youre a real C programmer, ready for a lucrative new career.
Compiling the Long Way
When we used the cc command, cc first compiled the program into an object module. Then cc linked the object module to create an executable program, the file named chap10. This is very important if you need to compile more than one file into your program. Most C programs require a number of .c files, all of which must be compiled and linked to form one program. One of the main reasons for separating C programs into multiple files is sanity: reading a 1MB program in one file is ludicrous. And yes, C programs get to this size, and even much bigger than 1 megabyte. Some C programs weve worked on include more than a million lines of C code. You need to know how to compile multiple .c files into one executable command because the vast majority of Linux freeware comes in this fashion.
To use the long method of compiling and linking, we split the tasks into two steps. First, you compile all the .c files you require. Then you link the resulting .o files (well get into this later) into your executable program. Because we have a very small C program typed in already (you did type it in, didnt you?), well start with that.
Compile chap10.c into an object module, an .o file, with the following command:
$ cc -c chap10.c
If you are successful, you should see a file named chap10.o in your directory. The .o file is called the object file (or object module); it contains unlinked machine code.
The next step is to link the object files (theres usually more than one) into an executable file. To do this, we again use the -o option to cc, but this time we pass a .o file at the end of the command line, rather than the .c file we used earlier:
$ cc -o chap10 chap10.o
This command links the file chap10.o into the executable program chap10. You can place more than one object filename on the command line, as in the following example:
$ cc -o chap10 chap10_a.o chap10_b.o chap10_c.o
Normally youll want to pick more descriptive filenames than the ones weve used.
Previous | Table of Contents | Next |