Makefiles are a way to manage all of the .c and .h files you have floating around. By now you might realize that programs can be made up of a bunch of different files. Here we start to use .o files. You should treat .o files like the machine language version of the .c file. For instance if you have a file called main.c, then the machine language compiled version would be the main.o file. The .o file is readable by the computer and the .c file is readable by the programmer.
driver.c
#include <stdio.h>#include "function.h" /*The function.h header file's contents is the function prototype.So you can use the function*/main(){ // declare variables int a, y, z; a = f(2); // the first function call y = 5; z = f(y); // the second function call /* Printing the Results To the Screen */ printf("a = %d \n", a); printf("z = %d \n", z);}function.h
int f(int x);function.c
f(int x) // This part is the function definition{ int result; result = x*x; return result // Alternatively, you could just use the code below // return x*x;}remember that at this point we can compile using
cc driver.c function.c -o appmakefile
# This is a target lineprog: driver.o function.o cc driver.o function.o -o app# This line is a dependency linedriver.o: function.h# The above line is needed because driver.o# has #include "function.h"on the command line we can now type the following:
make progthen we can run the program by typing
./appNOTE: prog is the name of the first target line and app is the renamed executable. These names can be chosen arbitrarily. You should also
Below is another make file for the guessing game using all the files the guessing game is made up of. Look at the utility target lines at the bottom. Typing 'make clean' or 'make real_clean' just executes bash commands.
In general, the target line execute bash commands. In the case where bash commands are dependent on certain files (like compiling .o files), the dependency lines are needed if there is a dependancy.
# This is the default target line to make the guessing gameguess: guess.o sequence.o test.o cc guess.o sequence.o test.o -o guess# These are the dependency lines for the object modules.# They show the .h dependencies used in each .c file.guess.o: guess.h play.h tfdef.hsequence.o: play.htest.o: play.h# These are utility target lines for cleaning up the directoryclean: rm -f *.oreal_clean: clean rm -f guess a.out core*