Programming in C - Linking
(Page 2 of 9 )
The linker combines the various modules generated by the compiler from source code files, adds required code modules from program libraries supplied as part of C, and welds everything into an executable whole. The linker can also detect and report errors—for example, if part of your program is missing or a nonexistent library component is referenced.
In practice, if your program is of any significant size, it will consist of several separate source code files, which can then be linked. A large program may be difficult to write in one session. By breaking it up into a number of smaller source files, you can make the development of the program a whole lot easier. The source files can be compiled separately, which makes eliminating simple typographical errors a bit easier. Furthermore, the whole program can usually be developed incrementally. Each source file will have its own file name, and the set of source files that make up the program will usually be integrated under a project name, which is used to refer to the whole program.
Program libraries support and extend the C language by providing routines to carry out operations that aren’t part of the language. For example, libraries contain routines that support operations such as performing input and output, calculating a square root, comparing two character strings, or obtaining date and time information.
A failure during the linking phase means that, once again, you have to go back and edit your source code. Success, on the other hand, will produce an executable file. In a Microsoft Windows environment, this executable file will have the .exe extension; in UNIX, there will be no such extension, but the file will be of an executable type.
In UNIX, the modules that are to be linked are given together with the cc command, for example:
cc myprog.c mod1.c mod2.o
These three modules will be linked. Notice that the last module here has the extension .o. This is because it has previously been compiled: the .o extension tells the compiler that the module is waiting to be linked and doesn’t need to be compiled again. The output of this stage is a file called a.out, which you should then rename to something more meaningful.
An alternative form to this is as follows:
cc -o myprog myprog.c mod.c mod2.o
This will compile and link the module myprog.c and create an executable file called myprog (defined straight after the -o flag).
Many C compilers also have a Build option, which will compile and link your program in one step. This option will usually be found, within an IDE, in the Compile menu; alternatively, it may have a menu of its own.
Executing The execution stage is when you run your program, having completed all the previous processes successfully. Unfortunately, this stage can also generate a wide variety of error conditions, ranging from producing the wrong output to sitting there and doing nothing, perhaps crashing your computer for good measure. In all cases, it’s back to the editing process to check your source code.
Now for the good news: this is the stage where, at last, you get to see your computer doing exactly what you told it to do!
In UNIX and DOS, to execute a program you just enter the name of the file that has been compiled and linked.
In most IDEs, you’ll find an appropriate menu command that allows you to Run or Execute your compiled program. This option may have a menu of its own, or you may find it under the Compile menu option.
In Windows, you can use Windows Explorer to locate the .exe file of your program, and then you can double-click the file.
The processes of editing, compiling, linking, and executing are essentially the same for developing programs in any environment and with any compiled language. Figure 1-1 summarizes how you would typically pass through each of these processes as you create your own C programs.
Creating Your First Program 
Figure 1-1.
Creating and executing a program It’s time to create your first program. Let’s step through the processes of creating a simple C program, from entering the program itself to executing the program. Don’t worry if what you type doesn’t mean anything to you at this stage—I’ll explain everything as we go along.
This article is excerpted from Beginning C by Ivor Horton (Apress, 2004; ISBN 1590592530). Check it out at your favorite bookstore today. Buy this book now. |
Next: Try It Out: An Example C Program >>
More Code Examples Articles
More By Apress Publishing