|
|
The most important of the tools discussed in these pages is the C and C++ compilation system, which translates C or C++ source code into the machine instructions of the computer your program is to run on. On the Unix operating system, the command to do this for C source code is cc:
$ cc mycode.cIf your program is in multiple source files, then the command is
$ cc file1.c file2.c file3.cand so on. As the examples suggest, the source files to be compiled must have names that end in the characters .c.
Similarly for C++ source code, the command is CC:
$ CC mycode.C
There are other things going on invisibly in these command lines that you will want to read about in ``C and C++ compilation system''. For now it's enough to note that either of these commands will create an executable program in a file called a.out in your current directory. The second command will also create in your current directory object files that correspond to each of your source files:
$ ls -1 a.out file1.c file1.o file2.c file2.o file3.c file3.oEach .o file contains a binary representation of the C language code in the corresponding source file. The cc command creates and then links these object files to produce the executable object file a.out. The standard C library functions that you have called in your program -- printf, for example -- are automatically linked with the executable at run time. You can, of course, avoid these default arrangements by using the command line options to cc that we describe in ``C and C++ compilation system''.
You execute the program by entering its name after the system prompt:
$ a.outBecause the name a.out is only of temporary usefulness, you will probably want to rename your executable:
$ mv a.out myprogYou can also give your program a different name when you compile it -- with a cc command line option:
$ cc -o myprog file1.c file2.c file3.cHere, too, you execute the program by entering its name after the prompt:
$ myprog