We create a new directory called pgm: bash-2.05b# mkdir pgm bash-2.05b# cd pgmIn this directory we create our first main program ``mainprog.c'':
#include <stdio.h>
int main(int argc, char* argv)
{ printf("I am the main program\n");
printf("I depend on nothing\n");
}
The make program knows what a C file is and it knows how
to compile a C file automatically. For example, we'll take a
few simple steps (and missteps). First we just try ``make'':
bash-2.05b# make make: *** No targets specified and no makefile found. Stop.Normally make will look for a file called Makefile or makefile. Neither was found so it complained. However, you can give command line arguments to make to request specific actions. Here we ask it to create mainprog.c
bash-2.05b# make mainprog.c make: Nothing to be done for `mainprog.c'.We asked make to do whatever it knows how to do to bring the file mainprog.c ``up to date''. This is a meaningless request since there is no program to change mainprog.c. We are the only ones who know how to change it. Lets try again.
This time we want the compiled form of mainprog.c which is the file mainprog.o. make knows how to produce a .o file from a .c file:
bash-2.05b# make mainprog.o cc -c -o mainprog.o mainprog.c bash-2.05b# ls mainprog.c mainprog.oYou can see from the above that make called the C compiler with two options: -c which asks for only the compile step and -o mainprog.o which names the output file. Thus make did what we asked. It produced the mainprog.o file.
make also knows how to call the linker (thru the compiler). Here we ask for the executable program to be made from the .o file:
bash-2.05b# make mainprog cc mainprog.o -o mainprog bash-2.05b# ls mainprog mainprog.c mainprog.oThis time make called the compiler with mainprog.o and asked -o mainprog for the output file to be called mainprog. In fact, we can now run it:
bash-2.05b# ./mainprog I am the main program I depend on nothing