Recitation - Jan 24, 2011
Compiling with g++
Make and Makefiles
- One use for a Makefile is to effeciently compile an executable.
- Reduce compile time by not recompiling source code that has not changed.
The following is known as a rule:
----------
target(s): prerequisite(s) (dependencies) (dependency list)
command(s) (sometimes called recipes)
----------
target(s): prerequisite(s) (dependencies) (dependency list)
command_1
command_2
command_3
.
.
.
command_n
target ... : dependency ...
command
...
...
...
Example:
----------
.PHONY : all clean
all: hello_world
hello_world: hello.cpp hello.h
g++ hello.cpp -o hello_world
# delete everything execpt source files
clean:
rm hello_world
rm *.o
A makefile consists of one or more Rules. Rules tell when and how to rebuild target(s).
The targets are sometimes called the goal of the rule.
make ultimately ensures that the goal(s) of the makefile are up-to-date.
Phony (Psuedo) Targets - target that does not have a real, corresponding file (file that we want to create).
all and clean are pseudo targets.
The pseudo targets will no longer work if a file with the
corresponding name really exists. Therefore, pseudo targets should be
explicitely stated in the Makefile.
To explicitely define a Psuedo Target, use the special, built-in
target name, .PHONY. (for other special built-in targets, see Special
Built-in Target Names).
To call a phony target, invoke make with the psuedo target name as the argument (e.g. make clean).
If no argument is specified, the first target (excluding targets
starting with a period) will be the default goal.
If no dependency is listed, then corresponding command(s) for that rule will always be executed.
To be safe, header files that are included in a given source file
should be listed as dependencies in the Makefile.
Bob Myers's howto makefiles
GNU make reference
Automatic variables
Debugging Example
Object files
- output function prototypes contained in the object file:
nm -C file.o
- T - defined in text (code) section
- U - undefined
- output prototypes and code of functions in the object file:
objdump -CS file.o
Other topics
const member functions
numeric value of the character data type
#include <iostream>
using namespace std;
int main()
{
char c;
// 200 = (binary)1100 1000
// 2^6 + 2^3 - 2^7
c = 200;
int out = c;
// out: -56
cout << "out: " << out << endl;
return 0;
}
implicit type conversion (coersion)
two's complement
conversion to destination type that is signed
- if source value can fit in destination type, the value will be unchanged.
- if source value cannot fit in destination type, the value is implementation-defined.
conversion to destination type that is unsigned
- if source value cannot fit, only the low order bits will be copied into the destination (high order bits that do not fit will be thrown away)
To warn about such problems using g++, use -Wconversion option (e.g. g++ -c -Wconverions file.cpp)
Whether char is signed or unsigned is defined by the implementation.
Therefore, if a char implemented using 8-bits takes on a value above 127, the
programmer should explicitely set the type as signed or unsigned.
wchar_t
- holds characters of larger character sets (e.g. Unicode)
- wide character (_t signifies standard typedef)
- usage: L'xy'
character literals (e.g. 'a') are symbolic constants for the numeric value of the character.
Programming Assignment #2 posted