COMP2004
Programming Practice
2002 Summer School


Kevin Pulo
School of Information Technologies
University of Sydney


(page 1)


Development Tools





(page 2)


Editing Code




(page 3)


Compiling Code




(page 4)


Benefits




(page 5)


Automating compilation


g++ -c file1.cc
g++ -c file2.cc
...
g++ -o prog file1.o file2.o ...


(page 6)


Shell scripts


chmod +x filename


(page 7)


Compilation shell script


#!/gnu/usr/bin/bash

g++ -Wall -g -c file1.cc
g++ -Wall -g -c file2.cc
g++ -Wall -g -o prog file1.o file2.o


(page 8)


Compilation shell script



#!/gnu/usr/bin/bash

g++ -Wall -g -ansi -pedantic -c file1.cc
g++ -Wall -g -ansi -pedantic -c file2.cc
g++ -Wall -g -ansi -pedantic -o prog \
file1.o file2.o


(page 9)


Shell variables



#!/gnu/usr/bin/bash

CXXFLAGS="-Wall -g -ansi -pedantic"

g++ $CXXFLAGS -c file1.cc
g++ $CXXFLAGS -c file2.cc
g++ $CXXFLAGS -o prog file1.o file2.o


(page 10)


More shell variables


#!/gnu/usr/bin/bash

CXXFLAGS="-Wall -g -ansi -pedantic"
LDFLAGS="-lm"

g++ $CXXFLAGS -c file1.cc
g++ $CXXFLAGS -c file2.cc
g++ $CXXFLAGS $LDFLAGS -o prog \
file1.o file2.o


(page 11)


Adding files


#!/gnu/usr/bin/bash
CXXFLAGS="-Wall -g -ansi -pedantic"
LDFLAGS="-lm"
FILES="file1 file2 file3 file4"

for file in $FILES; do
g++ $CXXFLAGS -c ${file}.cc
OBJS="$OBJS ${file}.o"
done
g++ $CXXFLAGS $LDFLAGS -o prog \
$OBJS


(page 12)


Environment variables



CXXFLAGS="-Wall -g"
export CXXFLAGS
export CXXFLAGS="-Wall -g"


(page 13)


Environment variables in C++



int main() {
string name = "CALENDAR_DATE";
char *c = getenv(name.c_str());
if (c) {
string s = c;
cout << "Today is: " <<
s.substr(0,3) << endl;
}
}


(page 14)


make




(page 15)


Makefile example


OBJS = main.o student.o course.o
CXXFLAGS = -Wall -g
LDFLAGS = -lm

all: prog
prog: $(OBJS)
$(CXX) $(CXXFLAGS) $(LDFLAGS) \
-o prog $(OBJS)
clean:
rm -f $(OBJS)


(page 16)


Using make


bash$ make cleanrm -f main.o student.o course.o
bash$ make OR make progg++ -Wall -g -c main.cc
g++ -Wall -g -c student.cc
g++ -Wall -g -c course.cc
g++ -Wall -g -lm -o prog main.o
student.o course.o
bash$


(page 17)


Example List Makefile


OBJS = main.o List.o
CXXFLAGS = -Wall -g
LDFLAGS = -lm

all: prog
prog: $(OBJS)
$(CXX) $(CXXFLAGS) $(LDFLAGS) \
-o prog $(OBJS)
List.o: List.h
test.o: List.h


(page 18)


makedepend




(page 19)


Example makedepend usage


SRCS = main.cc List.cc
OBJS = main.o List.o
CXXFLAGS = -Wall -g

depend:
makedepend -- $(CXXFLAGS) -- \
$(SRCS)



(page 20)


GNU make



SRCS = main.cc List.cc
OBJS = $(SRCS:%.cc=%.o)

SRCS = $(wildcard *.cc)


(page 21)


Searching Through Code




(page 22)


grep


grep
  • Most useful options:
    • -nprint line numbers
    • -vprint lines which don't match
    • -icase insensitive
    • -ccount number of lines


(page 23)


grep Regular Expressions




(page 24)


grep Example


int
convert42(int x) {
}
grep -n '^ *convert[0-9]+(' *.cc





























(page 25)


Debugging




(page 26)


gdb




(page 27)


gdb example


Segmentation fault (core dumped)


(page 28)


ddd and insight




(page 29)


Testing




(page 30)


How We Test




(page 31)


Run With Known Input




(page 32)


Saving the Output




(page 33)


Comparing Output



































































































































(page 34)