|
|
Now let's look at how this process works
for a C language program to print the words hello, world
.
Here is the source code for the program, which we have written in the file hello.c:
#include <stdio.h> main() { printf("hello, world\n"); }The code above is accepted by both the C and C++ compilers.
An alternative C++ source file that makes use of the C++ input/output classes would be:
#include <iostream.h> main () { cout << "hello, world" << endl; }
As indicated in ``Introduction to programming in standard C and C++'', the SCO OpenServer operating system command to create an executable program from C language source files is cc:
$ cc hello.cThe source files to be compiled by the C compiler must have names that end in the characters .c. C++ source files are compiled with the CC command:
$ CC hello.cThe source files to be compiled by the C++ compiler must have names that end in any of the following:
.c .C .cpp .CPP .cxx .CXX .CC .c++ .C++
Because there aren't any syntactic or semantic errors in the source code, either of the above commands will create an executable program in the file a.out in the current directory:
$ ls -1 a.out hello.cNote that a .o file is not created when you compile a single source file.
Execute the program by entering its name after the system prompt:
$ a.out hello, worldBecause the name a.out is only of temporary usefulness, we'll rename the executable:
$ mv a.out hellothen, invoke the program using this new name:
$ hello hello, world
You can also give the program the name hello when you compile it, with the -o option to the cc command:
$ cc -o hello hello.cor
$ CC -o hello hello.c
Execute the program by entering hello after the system prompt:
$ hello hello, world
Now let's look at how the cc and CC commands control the steps in the process described in ``Components of the C compilation system'' When you specify the -P option to cc or CC, only the preprocessor component of the compiler is invoked:
$ cc -P hello.cThe preprocessor's output -- the source code plus the preprocessed contents of the header file -- is left in the file hello.i in the current directory:
$ ls -1 hello.c hello.i
That output could be useful if, for example, you received a compiler error message for the undefined symbol a in the following fragment of source code:
if (i > 4) { / declaration follows int a; / end of declaration / a = 4; }The unterminated comment on the third line will cause the compiler to treat the declaration that follows it as part of a comment.
Because the preprocessor removes comments, its output
if (i > 4) {will clearly show the effect of the unterminated comment on the declaration. You can also use the preprocessed output to examine the results of conditional compilation and macro expansion.
a = 4; }
If you specify the -S option to the cc or CC command, only the preprocessor and compiler phases are invoked:
$ cc -S hello.cThe output -- the assembly language code for the compiled source -- is left in the file hello.s in the current directory. That output could be useful if you were writing an assembly language routine and wanted to see how the compiler went about a similar task.
If, finally, you specify the -c option to cc or CC, all the components but the link editor are invoked:
$ cc -c hello.cThe output -- the assembled object code for the program -- is left in the object file hello.o in our current directory. You would typically want this output when using make.
Enter the command
$ cc hello.oto create the executable object file a.out. By default, the link editor arranges for the standard C or C++ library function that was called in your program -- printf or cout -- to be linked with the executable at run time.
The outputs described above are, of course, inputs to the components of the compilation system. They are not the only inputs, however. The link editor, for example, will supply code that runs just before and just after your program to do startup and cleanup tasks. This code is automatically linked with your program only when the link editor is invoked through cc or CC. Also, CC automatically runs the prelinker to instantiate templates. Therefore, cc hello.o was specified in the previous example rather than ld hello.o. For similar reasons, you should invoke the assembler through cc or CC rather than as:
$ cc hello.sif you want to assemble and link hello.s.
As indicated in ``Introduction to programming in standard C and C++'', the compilation process is largely identical if your program is in multiple source files. The only difference is that the default cc or CC command line will create object files, as well as the executable object file a.out, in your current directory:
$ cc file1.c file2.c file3.c $ ls -1 a.out file1.c file1.o file2.c file2.o file3.c file3.o
If one of your source files fails to compile, you need not recompile the others. If you receive a compiler error diagnostic for file1.c in the above command line, your current directory will look like this:
$ ls -1 file1.c file2.c file2.o file3.c file3.oCompilation proceeds but linking is suppressed. Assuming you have fixed the error, the following command
$ cc file1.c file2.o file3.owill create the object file file1.o and link it with file2.o and file3.o to produce the executable program a.out. As the example suggests, source files are compiled separately and independently. To create an executable program, the link editor must connect the definition of a symbol in one source file with external references to it in another.
Not all the command line options discussed are compiler options. The -o option is actually an ld option that is accepted by the cc or CC command and passed to the link editor which creates the executable program. See the cc(CP), CC(C++) and ld(C) manual pages for more information.