The Compilation ProcessFrom Text to Binary ExecutableThis page outlines the basic compilation process and various tools that you will be using. We assume that you are using a Unix-derivative (Linux or other) or a Unix-like environment such as Cygwin. The basics are the same in all of these environments. When you run the gcc compiler a lot more is going on then you might think. This figure shows the major components in play.
Fig. 1. The main elements of the compilation process and the gcc tool chain.
The process starts with the use of a text editor or integrated development environment (IDE) to construct source code files. These are given the extensions .c and .h. These files are text and readable by any text editor. In this course we will not be using an IDE. The steps are:
Later in the course we will see some variations on this basic scheme. GCC can be given different instructions at the command line to cause it to behave differently as needed. We will also add another tool called a debugger ( Contents and Relations of Files in a C ProgramFor right now there are just two kinds of files that you will create for your C programs. As you get started you will probably only create one of each for simple programs, but as the programs get more complex you will need to break it up into discrete modules based on a logical grouping of functions.
For every C program there is a single .c source code file given the name of the program, e.g. “hello” is the final program, so the main file is called
In Figure 2 we see a program that has been divided up into a number of files:
Fig. 2. Showing the relations of text files in a typical C program.
The other two module files (along with their header files) are similar in containing functions that can be called by anywhere in the program (e.g.
For example, lets say that the
Main Menu
1. Load a File
2. Edit
3. Save
4. Delete a File
5. Exit
Choose a number option: ___
This menu of options is displayed in an endless loop. If the user chooses #5 then the program will terminate normally by breaking out of the loop. If the user selects any other option, say #1, the code calls a function in module1.c called loadFile(). However, in order to print the menu on the screen in the first place, suppose there is a function in module3.c called formatALine(), that takes some arguments that allow it to produce a formatted line to be printed by main() as it prints the menu. That function is what is called a helper function that can be called from many different places in the overall program to do a specific job that is needed by many other functions.
//in program.c
int main(void) {
// initialize variables
// enter endless loop
for(;;) {
// print menu
// get selection
switch selection {
case 1: errorNo = loadAFile();
case 2: errorNo = editAFile();
// etc.
}
}
}
// in module1.c
int loadAFile(void) {
char filename[91];
int errorCode = NO_ERROR; // #defined as 0 in program.h
printf("Enter a file name: ");
scanf("%s", filename); // remember filename is actually the address of the array!
errorCode = checkFileName(filename);
// do other stuff, like writing the filename to a shared location in memory
return errorCode;
}
// in module3.c
int checkFileName (char * filename) { // interesting case! the identifier "filename" here is
// actually different from the same name in the calling
// function above!
int errorCode = NO_ERROR; // this errorCode is LOCAL to this function!
// do stuff to check that the filename is in proper format, exists on the disk, etc.
return errorCode;
}
In the above code “snippets” we see that the
The standard library functions (e.g. standard I/O functions declared in
|