| Debugging C and C++ code in a Unix environment | ||
|---|---|---|
| Prev | Chapter 3. Aspects of debugging C and C++ code | Next |
Bugs you encounter may not be due to your C or C++ code; they might be the result of how your executable/library was built. Make sure that you understand how the build process is organised.
You should use a Makefile. A Makefile describes how to build your project: it lists the files involved in your project, their interdependencies and how a tool should build intermediary files and the end product. Make sure you have listed all dependencies; missing even a single dependency can lead to subtle problems.
make is a powerful tool, and it pays off to acquaint yourself with it well. For instance, in general you should not list compilation lines directly. GNU make has some builtin rules (so called implicit rules) on how, say, .o files are built from .c files. To use those rules, you only specify the dependencies (e.g. foo.o: foo.c foo.h bar.h (for C or foo.cc for C++ programs)), and no build rule. The implicit rules have a number of variables that you can set (e.g. CC for the C or C++ compiler, CFLAGS for the compilation flags, LOADLIBES for the libraries). Using the implicit rules makes your makefiles shorter, easier to read and easier to modify. See [MAKETUT], [MAKETUT2] and [MAKE] for details on make.
For C and C++, the programs involved in building and running programs are:
The preprocessor's main task is to process (header (.h)) file inclusions and macros; it outputs pure C or C++ code.
The compiler translates pure C or C++ code to assembly language.
The assembler translates assembly code to binary object code (.o).
The linker combines a number of object files and libraries to produce an executable or library. If this executable or library needs no external libraries, it is called statically linked; otherwise it is called dynamically linked.
The dynamic loader's task is to load the libraries (or library parts) required by a dynamically linked executable prior to actually running that executable.