All these types of errors refer to different stages of program’s lifetime. To understand these errors, we have to understand the stages of a program’s lifetime. Generally the programs are written in high level programming languages like C or Fotran which is referred as source codes. Source codes are human readable text that are written in English (or other) like language. But the source codes can not get directly executed because the processors understand only machine language. Source codes go through different stages to eventually be converted int executable (machine language code) that can be loaded and run to accomplish tasks defined in the source code. The stages are: pre-processing, optimization, compiling, linking, running etc. There might be other intermediate stages also. We’ll be interested in compiling, linking and running to understand errors of these stages.
Compilation error
Compilers convert the source code into object code. As mentioned above source code is what the developer writes in high level programming language. But compiler can not convert any source code to object code. Every programming language has its own predefined constructs and syntax. Developers have to follow those constructs and syntax while writing the program. Any violation of this will lead to compilation error. Compiler will fail to generate object code if any compilation error occur.
Example: I think every programmer faced compilation error. I can’t imagine a programmer can write perfect compilation error free code every time. So, every programmer is very much familiar with compilation errors. Still I gave one example of such errors.
The following program prints all elements of an array. There is no compilation error in this program.
Note: I chose C programming language for the examples in this document and Linux environment (cc compiler) for compilation and running. Examples will be similar for other languages.
#include <stdio.h> void main() { int arr[] = {1, 4, 9, 3}; int i; for(i = 0; i < sizeof(arr)/sizeof(int); i++) { printf("arr[%d] = %d\n", i, arr[i]); } }
Output of the program:
cc -o test.o -c file1.c cc -o test test.o $ ./test arr[0] = 1 arr[1] = 4 arr[2] = 9 arr[3] = 3
Now from the above program, one semicolon (“;”) is removed from the for loop. So I violated the syntax of for loop of C language. Here is the modified code.
#include <stdio.h> void main() { int arr[] = {1, 4, 9, 3}; int i; for(i = 0 i < sizeof(arr)/sizeof(int); i++) { printf("arr[%d] = %d\n", i, arr[i]); } }
If I compile the above program the compilation will failed with the following error.
cc -o test.o -c file1.c file1.c: In function âmainâ: file1.c:7:15: error: expected â;â before âiâ for(i = 0 i < sizeof(arr)/sizeof(int); i++) ^ file1.c:7:47: error: expected â;â before â)â token for(i = 0 i < sizeof(arr)/sizeof(int); i++) ^
Linking error
Linking is a process to convert one or more object files to an executable binary (machine language program). During the process all symbols, such as function calls or external variables etc are resolved. Missing of any symbol will lead to linking error. For example, if some function is used in the program but that is not defined anywhere in the program or in the included libraries.
Example: In this example, we have two files. In file1.c we have the main() function. Inside main() two functions func1() and func2() are called. The functions are defined in another file file2.c.
file1.c:
#include <stdio.h> void main() { func1(); func2(); }
file2.c:
#include <stdio.h> void func1() { printf("Inside func1\n"); } void func2() { printf("Inside func2\n"); }
The output is:
cc -o test1.o -c file1.c cc -o test2.o -c file2.c cc -o test test1.o test2.o ./test Inside func1 Inside func2
Now the func2() is removed from the second file file1.c.
file2.c:
#include <stdio.h> void func1() { printf("Inside func1\n"); }
If I try to compile these files, both the files will be compiled successfully because no syntax or programming construct is violated in source files.
cc -o test1.o -c file1.c cc -o test2.o -c file2.c
The object files, test1.o and test2.o are generated in current directory.
But when I tried to link the object files, the following errors will be generated.
cc -o test test1.o test2.o test1.o: In function `main': file1.c:(.text+0x14): undefined reference to `func2' collect2: error: ld returned 1 exit status
When the linker tried to resolve all the used symbols, func2() was not found anywhere in any object files.
Run time error
Run time error occurs in time of the execution of the program. In this case program code is compiled and linked successfully but encounters problems when it runs. Divide by zero or accessing memory which is not allowed are the examples of run time errors. Run time error leads to program crash most of the time. Some languages have the provision of exception handling to tackle program crash.
Example: The following program has an arithmetic expression where one variable is dependent on user input. The program has no problem in compilation and linking but crashes for one input (5).
#include <stdio.h> void main() { int n; int result; printf("Enter a number: "); scanf("%d", &n); result = 200 / (5 - n); printf("result=%d\n", result); }
Output:
[polaris@localhost del]$ cc -o test file1.c [polaris@localhost del]$ ./test Enter a number: 3 result=100 [polaris@localhost del]$ ./test Enter a number: 5 Floating point exception (core dumped)
This program works fine all inputs but for 5 it encounters divide by 0 error.