Discussion

Up

Whats New
Class Info
Calendar
Lecture
Discussion
Homework
Laboratory
TA Corner
FAQ

CS-12 Discussion Section

Final Review

Make Files

Shell Scripts

Make Files

Here are source files for prog1.c, prog2.c, a.c, b.c, and c.c. Also in the mix is the header file abc.h.

A makefile (called makefile with no extension) is used to compile the program prog 1 and prog2.

We invoke the compilation process by calling the make program and feeding it the makefile via the -f switch as shown below.

C:\Gcc>make -f makefile

The resulting output is

gcc -I. -c a.c
gcc -I. -c b.c
gcc -I. -c c.c
gcc -I. -o prog1 prog1.c a.o b.o c.o -lm
gcc -o prog2 prog2.c a.o b.o c.o -lm

 

The text of the make file is shown below. You should be able to explain what each line does.

CFLAGS = 

CC = gcc
LIBS = -lm 
INCLUDES = -I. 
OBJS = a.o b.o c.o
SRCS = a.c b.c c.c prog1.c prog2.c
HDRS = abc.h

all: prog1 prog2

# The variable $@ has the value of the target. In this case $@ = psort
prog1: prog1.c ${OBJS}
${CC} ${CFLAGS} ${INCLUDES} -o $@ prog1.c ${OBJS} ${LIBS}

prog2: prog2.c ${OBJS}
${CC} ${CFLAGS} -o $@ prog2.c ${OBJS} ${LIBS}

.c.o:
${CC} ${CFLAGS} ${INCLUDES} -c $<

depend: 
makedepend ${SRCS}

clean:
rm *.o core *~ prog1 prog2

tar:
tar zcf code.tgz Makefile *.c *.h testfile1

print:
more Makefile $(HDRS) $(SRCS) | enscript -2r -p listing.ps
# DO NOT DELETE

 

Shell Scripts

Shell scripts were presented on several occasions in discussion section. The following is a shell script to compute the factorial of a number. Be sure you know how it works.

#!/bin/sh
# this script accepts a number from the user and compute its factorial
fac()
{
if [ $1 -gt 1 ]; then
NEXT=`expr $1 - 1`
REC=`fac $NEXT`
PROD=`expr $1 \* $REC`
echo $PROD
else
echo 1
fi
}

echo "Enter a number: "
read NUM
echo $#
echo $@
echo "$NUM! = `fac $NUM`"