First | Next | Previous | Last | Glossary | About |
Most of the work we have done has been fairly straightforward in terms of compiling and linking you programs.
We have used an editor to create a source file. Then we run the compiler which generates an executable file for us.
There are other phases in program building. We probably haven't seen in them action but they are definitely there.
Although the compiler appears to be doing all the work it is actually sharing it out.
Prior to the actual compilation there is a preprocessor phase when CPP (the C Pre-Processor) reads header files and does the necessary includes and defines and so on.
After the compile phase there is the link phase. This is the time when all the various libraries that are required by the program are "linked" with your object code.
A more complex project may contain many source files and may use many libraries.
Figure 2 shows a simple project which is based on two source files and uses a library.
As you can imagine there is quite a deal of work going on in the background and in order to understand what is happening we need to digress a little from the pure programming task. We need to look more closely at the mechanics of program building and learn how to use some tools which will improve our programming skills.
Both figures 1 and 2 show some new concepts, amongst them:
object files
libraries
linking
You have already done some work with the pre-processor, you might like to review it here. We'll do more work with the pre-processor and also investigate the new topics listed above.
To this list we have to add a couple more items:
building libraries
using make
debugging programs
profiling programs
A program can exist in several forms. It can be in source code form, the form created by the programmer. It can be in object form, the form created by the compiler and it can be in executable form, the form created by the linker.
gcc -o simple simple.c
Imagine we have built a simple program using the command line shown here. The result will be a program called simple.exe (in windows, just simple in Linux).
0E 1F BA 0E 00 B4 09 CD-21 B8 01 4C CD 21 54 68 69 73 20 70 72 6F 67 72-61 6D 20 63 61 6E 6E 6F 74 20 62 65 20 72 75 6E-20 69 6E 20 44 4F 53 20 6D 6F 64 65 2E 0D 0D 0A-24 00 00 00 00 00 00 00 50 45 00 00 4C 01 03 00-BA A1 A2 38 00 00 00 00 00 00 00 00 E0 00 0F 01-0B 01 02 37 00 04 00 00 00 04 00 00 00 00 00 00-C4 11 00 00 00 10 00 00 00 20 00 00 00 00 40 00-00 10 00 00 00 02 00 00
If you were to do a simple dump of the program you might see something like this. It is the contents of the executable, or at least a small part of it.
It is a hexidecimal display of the program code but this time it is in a form understood by the CPU, almost. You know of course that the CPU works with bits and not hex codes.
gcc -o simple.c
We get a similar kind of result with the command to create an object file. The result of this compiler command is a file called simple.o. I won't show you result, it's about as informative as the last one. In both cases we have a program file that is closer to the machine than the original source file.
The difference between the two is significant though. The first case, the executable, represents a finished product. It has been compiled and linked. It has had all its references resolved.
The second case, the object file, is a part of a program on the way to completion. There may be many object files which need to be linked together.
The linking process will do several things, in the words of the GNU Linker Overview it "combines a number of object and archive files, relocates their data and ties up symbol references". What this means is that all those calls to functions in the object files you have written are tied together. All the variables are gathered into the right places and the libraries that contain the standard functions and classes are combined with your program.
gcc -o simple simple.c notsosimple.o -lfltk.a
This command compiles simple.c and links the object files simple.o, notsosimple.o and the library fltk.a.
First | Next | Previous | Last | Glossary | About |
Copyright © 1999 - 2001
David Beech