Exercise 3-35 (Exercise 3-1, make and make debug)
Chapter_3 Exercise_3-34 Exercise_3-30 | ch3-Exercises Chapter_4 |
Exercise 3-35 TCP1, p. 231
Exercise 3-35. Create a makefile for one of the previous exercises (of your choice) that allows you to type make for a production build of the program, and make debug for a build of the program that includes debugging information.
Notes: We decided to use the files from Exercise_3-1 (functions.hpp, functions.cpp, main.cpp). See also ch1-debug on the blog Advanced_Linux_Programming, Chapter_1.
Folder ex3-35 contains the following files:
ReadMe.txt
Makefile
functions.hpp
functions.cpp
main.cpp
ReadMe.txt download
Open the terminal in folder ex3-35.
Compile program:
make // make all
g++ -c functions.cpp
g++ -c main.cpp
g++ functions.o main.o -o main
Run program:
make run
./main
void f (void)
char g (char a, int 0)
int h (int 0, float 1.2)
int overloaded (float 2.1)
void overloaded (int 1, int 2)
char overloaded (char a, int 0, int 1, float 2.1)
Delete object files:
make clean
rm *.o
Compile program with debugging information:
make CPPFLAGS=-g
g++ -g -c functions.cpp
g++ -g -c main.cpp
g++ functions.o main.o -o main
make debug
g++ -g -c functions.cpp
g++ -g -c main.cpp
g++ functions.o main.o -o main
Run program with debugging enabled:
make run
./main
void f (void)
char g (char a, int 0)
int h (int 0, float 1.2)
int overloaded (float 2.1)
void overloaded (int 1, int 2)
char overloaded (char a, int 0, int 1, float 2.1)
Makefile download
CPP = g++
OFLAG = -o
# debug flag:
DBGFLAG = -g
.SUFFIXES : .o .cpp
.cpp.o :
$(CPP) $(CPPFLAGS) -c $<
all: main
debug: functions.cpp main.cpp
$(CPP) $(CPPFLAGS) $(DBGFLAG) -c functions.cpp
$(CPP) $(CPPFLAGS) $(DBGFLAG) -c main.cpp
$(CPP) functions.o main.o $(OFLAG) main
run: main
./main
main: functions.o main.o
$(CPP) functions.o main.o $(OFLAG) main
functions.o: functions.cpp
main.o: main.cpp
clean: # delete object files
rm *.o
Chapter_3 Exercise_3-34 Exercise_3-30 | BACK_TO_TOP | ch3-Exercises Chapter_4 |
Comments
Post a Comment