the make utility helps us
the make utility
input is a file containing rules
the rules specify what to do
(and under what circumstances)
name the file containing the rules:
makefile or
(preferred) Makefile
Format of a Makefile
target1: list of dependencies command1 command2 target2: list of dependencies command3 command4
Each command is preceded by a tab.
Sample Makefile
my_binary: binary.o list.o stack.o g++ -o my_binary binary.o list.o stack.o binary.o: binary.cpp binary.h common.h g++ -c binary.cpp list.o: list.cpp list.h common.h g++ -c list.cpp stack.o: stack.cpp stack.h common.h g++ -c stack.cpp clean: rm binary.o list.o stack.o reallyclean: rm my_binary binary.o list.o stack.o
Defining macros (variables)
OBJ = binary.o list.o stack.o my_binary: $(OBJ) cc -o my_binary $(OBJ) binary.o: binary.cpp binary.h common.h g++ -c binary.cpp list.o: list.cpp list.h common.h g++ -c list.cpp stack.o: stack.cpp stack.h common.h g++ -c stack.cpp clean: rm core $(OBJ) reallyclean: rm my_binary core $(OBJ)
Other things that make does,
but we do not have time to discuss
purify
helps find memory mismanagement
compile program for use with purify
purify g++ -g one.cpp two.cpp
running instrumented program
reports errors encountered
purify on a small example
testObject.h
#ifndef TESTOBJECT_H
#define TESTOBJECT_H
using namespace std;
class testObject {
public:
testObject(); // constructor
~testObject(); // destructor
void printObject();
private:
int * intptr; // dynamically allocated
// integer, or array
int x;
};
#endif
testObject.cpp
#include <iostream>
#include "testObject.h"
using namespace std;
// constructor
testObject::testObject() {
cout << "testObject constructor invoked" << endl;
intptr = new int;
*intptr = 65;
x = 64;
}
// destructor
testObject::~testObject() {
cout << "testObject destructor invoked" << endl;
// code missing, which causes a memory leak:
// delete intptr;
}
// print x and what intptr points to
void testObject::printObject() {
cout << "*intptr is " << *intptr << endl;
cout << "x is " << x << endl;
}
testpurify.cpp
#include#include "testObject.h" using namespace std; int main() { char temp; // cause a memory leak cout << "memory leak test. Hit 'y' to continue:"; cin >> temp; if ( temp == 'y' ) { testObject T1; T1.printObject(); } return 0; }
the xterm
purify's window
the xterm, as the program finishes
purify's window
purify's window, after clicking on
"Memory leaked", and then "MLK:"
Debuggers
they help us debug our code
(for example, when the program crashes)
gdb -- the GNU debugger
a source-level (symbolic) debugger
has a command-line interface
compile program using -gdb flag
g++ -ggdb one.cpp two.cpp
run the executable with
gdb a.out
| Copyright © Karen Miller, 2007, 2008 |