cc main.cgenerates the executable file a.out in the current directory. Then, typing
a.outwill execute the compiled program.
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.cThe order of the .c 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. The result is 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, because 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 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 line following prog: contains the Unix command which will build the target file, provided that all the prerequisite files exist.
Note that the label must begin in column 1 and 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; assuming that sin.c #includes the file sin.h, we list that file too. We compile sin.o with the -c flag because sin.c we wish to create a .o file but not an executable program.
makeThis causes make to look for a file named Makefile in the current directory.
Make checks whether all the specified files exist and whether the derived files are up-to-date. A file like sin.o is out of date if the creation time of the file sin.o is earlier than the time of last modification for either of the files sin.c or sin.h.
If derived files are out of date or non-existent, make issues the commands shown below each selected target.
# 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: lpr $(SRCFILES) 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