DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Introduction to programming in standard C and C++

C and C++ compilation

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.c
If your program is in multiple source files, then the command is
   $ cc file1.c file2.c file3.c
and 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.o
Each .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.out
Because the name a.out is only of temporary usefulness, you will probably want to rename your executable:
   $ mv a.out myprog
You 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.c
Here, too, you execute the program by entering its name after the prompt:
   $ myprog

Next topic: C language
Previous topic: Program analysis

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