C and C++ compilation system

Basic cc and CC command line syntax

Now let's look at how this process works for a C language program to print the words hello, world.

hello world program

Here is the source code for the program, which we have written in the file hello.c:

   #include <stdio.h>
   	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;

Creating the executable

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.c
The 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.c
The source files to be compiled by the C++ compiler must have names that end in any of the following:

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
Note that a .o file is not created when you compile a single source file.

Executing the program

Execute the program by entering its name after the system prompt:

   $ a.out
   hello, world
Because the name a.out is only of temporary usefulness, we'll rename the executable:
   $ mv a.out hello
then, invoke the program using this new name:
   $ hello
   hello, world

Specifying a different executable name

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.c
   $ CC -o hello hello.c

Execute the program by entering hello after the system prompt:

   $ hello
   hello, world

Invoking the preprocessor only

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.c
The 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

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.

NOTE: The C++ compiler will identify the bad comment and tell you about it.

Because the preprocessor removes comments, its output

   if (i > 4)

a = 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.

Invoking the preprocessor and compiler only

If you specify the -S option to the cc or CC command, only the preprocessor and compiler phases are invoked:

   $ cc -S hello.c
The 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.

Suppressing the linker phase

If, finally, you specify the -c option to cc or CC, all the components but the link editor are invoked:

   $ cc -c hello.c
The 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.

NOTE: See Software development tools for further information regarding make.

Enter the command

   $ cc hello.o
to 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.

NOTE: If you are compiling C++ source files, they must be linked with CC. C source files can be linked with either cc or CC.

Passing files to cc and CC

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.s
if you want to assemble and link hello.s.

Compiling multiple source files

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

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
Compilation proceeds but linking is suppressed. Assuming you have fixed the error, the following command
   $ cc file1.c file2.o file3.o
will 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.

A note on command line options

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.

Next topic: Commonly used command line options
Previous topic: Organization of C++ Compilation System

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005