Creating a Makefile ------------------- A Makefile is used for automatically compiling and linking your programs together. The first homework assignment used a Makefile for the C++ code. Here is a sample Makefile: # makefile for gl prog10.c CC= cc # flags CFLAGS= -O LDFLAGS= -s MODULES= LIBS= -lgl_s -lc_s default: prog10 # main compile module prog10: ${MODULES} prog10.o ${CC} ${LDFLAGS} ${MODULES} prog10.o -o prog10 ${LIBS} # source compilations prog10.o: prog10.c ${CC} -c ${CFLAGS} prog10.c # remove .o files clean: rm *.o Rules about Makefiles are as follows: 1. Type everything as it is into a file called "Makefile" except the lines after the following lines: prog10: ${... prog10.o: prog... clean: The line that follows each of these line must start with a . For example, ${CC} ${LDFLAGS} ${MODULES} prog10.c -o prog10 ${LIBS} 2. The lines starting with a "#" sign are comment lines. 3. Blank lines are ignored. 4. The line containing variable assignment CC= cc is the way to assign a compiler. These variables are not shell variables, but a special type of variables used only in Makefiles. You do not necessarily need to use these variables, but I suggest using this Makefile as a template and modifying it for the projects you work on in the near future. Different options here for compilers are: cc Standard C compiler CC Standard C++ compiler gcc GNU C compiler g++ GNU C++ compiler Note: If you want to use gcc as your compiler for your gl C code, you will need to include the -lX11_s library option to the variable called LIBS (further down). 5. The line containing CFLAGS is for compiling flags which you so far haven't used when compiling. The one used in this Makefile is the -O option, which tells the compile to optimize at the default level. Optimization makes things go slightly faster. You may even use different levels such as -O2 or -O3. Depending on the machine you are on. 6. The line containing LDFLAGS is for linking flags which you also have not used so far. In this case just accept the flag they use called -s which basically is involved with the linking of the .o files. 7. The line containing MODULES is for adding additional C modules such as in the last assignment (Part 3). There were 3 different modules besides the main module. In this example, there are no additional modules. 8. The line containing LIBS is for including the libraries that you wish to use. You already have been using the shared C library, -lc_s, and the shared gl library, -lgl_s. These are listed here. 9. The line containing default: is the line where you put your default compile program. In other words, when you type "make" it looks for this default line and uses "prog10" in this case. If this line is not there, you would have to type "make prog10". This line is useful in case you have more than one program you wish to compile. 10. The next section is where you put your default main compile module. Always use the name that you are going to call when you use "make name" or in the default: line when you use "make" first. The format is: name: ${MODULES} name.o ${CC} ${LDFLAGS} ${MODULES} name.o -o name ${LIBS} 11. The next section is where you put the source compilation part. The format is: name.o: name.c ${CC} -c ${CFLAGS} name.c : : 12. The last section is for removing old .o files. All you need to do is leave this part like it is. Be sure to use the before the "rm *.o" command. You need not use the name "clean:" as well. You could put "erase:" instead or whatever you like. A more complicated Makefile looks like this (this is the one used for the homework project #1 and Part 3 of Homework #4): # # makefile # CC= g++ # # flags for cs machines # CFLAGS= -O LDFLAGS = -s # AM= am_gl.o am_read.o LIBS= -lmalloc /usr/lib/libimage.a -lfm_s -lgl_s\ -lX11_s -lm -lc_s # default: am #### Art Maker Main Compile #### am: ${AM} am.o ${CC} ${LDFLAGS} ${AM} am.o -o am ${LIBS} #### Vision Studio Object Compile #### am.o: am.cc ${CC} -c ${CFLAGS} am.cc am_gl.o: am_gl.cc ${CC} -c ${CFLAGS} am_gl.cc am_read.o: am_read.cc ${CC} -c ${CFLAGS} am_read.cc #### CLEAN .o FILES #### clean: rm *.o Try typing in the first one. You will be using it in the next project.