cc main.cgenerates an executable file named "a.out" in the current directory.
a.outas a command will run it.
cc -o myprogram main.ccreates an executable file named `myprogram' (and using myprogram as a command name will run it).
cc -o prog main.c sin.c menu.cwhere all the `.c' files have been listed in the command line. The order of the file names is irrelevant.
#include "sin.h"it looks in the current directory for a file named sin.h and reads that.
cc -o prog -I../include main.c sin.c menu.cwould cause the C compiler to look in a sibling directory named ``include'' for the `.h' files.
cc -c main.cthe C compiler creates a binary object file named main.o. The main.o file is not executable (because it needs to be linked with other modules and library functions before it is a complete program).
cc -c main.c cc -c sin.c cc -c menus.cThe order is not important. We get three `.o' files. These `.o' files can be linked using one further invocation of the cc command:
cc -o prog main.o sin.o menus.o
cc -c sin.c cc -o prog main.o sin.o menus.oNote that we do not have to recompile main.c or menus.c (they did not change).
For large programs with many modules, separate compilation is much faster than recompiling everything.
man cc
# Makefile for the prog program prog: main.o sin.o menus.o cc -o prog -I../include main.o sin.o menus.o main.o: main.c sin.h menus.h cc -c -I../include main.c sin.o: sin.c sin.h cc -c -I../include sin.c menus.o: menus.c menus.h cc -c -I../include menus.cThe `#' character introduces a comment; the comment continues up to the end of the line.
The label `prog:' (in line 3) defines a target. To build this target we need the files listed after the label, i.e. we need the files `main.o' `sin.o' and `menus.o'. These are called the prerequisites for the target. The following line contains the Unix command which will build the target file, provided that the prerequisite files all exist.
Note that the label must begin in column 1 of a line. Note that the lines containing Unix commands must start with a tab character. (Spaces do not work.)
A prerequisite like `sin.o' may not exist (or may need to be
re-created). Therefore, our Makefile contains lines that
define `sin.o' as a target. To create `sin.o', we require a
source code file `sin.c' -- and presumably `sin.c' has a
#include of the file `sin.h', so we list that file too.
The Unix command to create `sin.o' is cc with the -c flag.
makeThis causes make to look for a file named `Makefile' (or `makefile') in the current directory.
If derived files are out of date or non-existent, make issues
the necessary Unix commands to re-create them as necessary.
# Makefile for the prog program INCLDIR = ../include prog: main.o sin.o menus.o cc -o prog -I$(INCLDIR) main.o sin.o menus.o main.o: main.c sin.h menus.h cc -c -I$(INCLDIR) main.c sin.o: sin.c sin.h cc -c -I$(INCLDIR) sin.c menus.o: menus.c menus.h cc -c -I$(INCLDIR) menus.c
# Makefile for the prog program INCLDIR = ../include SRCFILES = main.c sin.c sin.h menus.c menus.h prog: main.o sin.o menus.o cc -o prog -I$(INCLDIR) main.o sin.o menus.o main.o: main.c sin.h menus.h cc -c -I$(INCLDIR) main.c sin.o: sin.c sin.h cc -c -I$(INCLDIR) sin.c menus.o: menus.c menus.h cc -c -I$(INCLDIR) menus.c print: $(SRCFILES) lpr main.c sin.c sin.h menus.c menus.h clean: rm -f *.o coreWith this Makefile, the user can now type the command:
make printand the command to send all the source files to the printer will be executed. (The Make command is fooled into thinking that you want to create a file named `print'; when it finds that this file does not exist, it runs the Unix command listed below the `print' target.)
Similarly, the command
make cleanexecutes a command to remove all non-essential files. (The `.o' files are expendable because they can be re-created by running the C compiler; a large file named `core' is a common result of testing buggy programs.)
foo: main.c foo.o bar.o jack.o jane.o jill.ocould have been typed as
foo: main.c foo.o \ bar.o jack.o \ jane.o jill.o(and similarly for any other line in a Makefile).