Next Previous Contents

7. Compiling and Executing Programs in C and C++

7.1 C

Using any editor, create a source file with a name of the form something.c

Use the cc command to compile, assemble, and link you program:

        cc something.c

The resultant executable file will be called a.out. You can either run it as it is:

        a.out

or you can first rename it to something more meaningful (which is preferable):

        mv a.out something
        something

The executable can also be given the desired name during linking:

        cc -o something something.c
        something

7.2 C++

The same as for C, except use CC rather than cc for the compiler.

7.3 GNU C, C++

Many Unix systems now have C/C++ compilers developed by the Free Software Foundation's GNU project (GNU stands for GNU's Not Unix). Many users prefer the GNU C compiler to the standard compiler that comes with the Unix distribution, and often GNU C++ is the only version of C++ available.

Using any editor, create a source file with a name of the form something.c (C) or something.cc (C++)

Use the gcc or g++ command to compile, assemble, and link your program:

        gcc -o something something.c

or

        g++ -o something something.cc

7.4 Linking Multiple Files

The above discussion has assumed your entire program resides in a single source file. If this is not the case, you have several options:

  1. You can specify any number of source files on a single command line (provided they are all in the same language.) Example:
                    cc -o foo foo.c bar.c baz.c
            
    
    This will compile the three source files foo.c, bar.c, and baz.c into object code and will then link the resultant object code into a single executable foo.
  2. You can compile each source file to an object file separately (using the -c option on the compiler command line), and then link them together. Example:
                    cc -c foo.c
                    cc -c bar.c
                    cc -c baz.c
                    cc -o foo foo.o bar.o baz.o
            
    
    The first three commands produce object files foo.o, bar.o, and baz.o. The latter links them into one foo file (but also leaves the object files available for later use.)
  3. You can combine the above approaches. Example: Suppose you have already compiled bar.c into bar.o and baz.c into baz.o, but need to compile foo.c (perhaps because you have edited it recently). You could use the following command:
                    cc -o foo foo.c bar.o baz.o
            
    

7.5 Using Make

Long before Borland and Microsoft started developing integrated development environments for program development, Unix was using a utility called make to manage the task of building programs consisting of multiple source files and/or libraries. The make program uses a specification file, usually named Makefile or makefile, that describes the dependencies between the source files, object files and executables.

The entries in the makefile consist of a line indicating a dependency relationship followed by one or more lines, each of which begins a TAB character, with commands to build the dependent file.

A makefile for the example above might like look like

foo:    foo.o bar.o baz.o
        cc -o foo foo.o bar.o baz.o

foo.o:  foo.c
        cc -c foo.c

bar.o:  bar.c
        cc -c bar.c

baz.o:  baz.c
        cc -c baz.c

Typing make at the prompt will cause the file foo to be created.

Make knows how to build C and C++ source files, so the only lines really necessary are the first two -- make will automatically realize that to get foo.o the file foo.c must be complied with the C compiler.

Now if the file foo.c has just been edited and you type make, only foo.c will be recompiled before the executable is relinked.

Read the manual page for make for more information.


Next Previous Contents