The errors
Compile Error
Suppose my source code forgets to include the file <stdio.h> (which is declared the printf), or ";" I will then an error message compilation (syntax error, parse error. ..). These are the classic mistakes.
Error linkage
These errors are more subtle because they do not syntax, but how is structured and compiled the program. They are easy to recognize when using gcc and g + + because messages about errors ld (the link).
Example of Multidefination
A linkage error can occur when you write the code of a program using multiple files. We will now illustrate this kind of mistake.
Suppose that my program is written through 3 files ah, ac, and main.c. The AH header is included by both files and source main.c ac main.c The file contains the main ().
1) If I just compile ac, does not file containing hand, we must specify the compiler (-c option in GCC) otherwise it does not know how to create an executable, since there are no as a starting point. That is why the file containing the main (no-c option) and other sources compiling files differently. These are:
Code:
gcc-Wall-W ac-c
gcc-W-Wall-o plop.exe main.c ao
The options-W-Wall can display more messages of warning.
- The first order ao built from ac
- The second generation combined with the binary main.c, the assembling ao, and produces an executable (plop.exe)
We can see immediately that if the program has an error in action, the compiler will trigger an error when compiling This action will cause cascading errors in other files. That is why when a program does not compile, it starts with the first error messages, they are resolved, so you compile ... until all errors are resolved.
2) Remember that in normal times, it said the function in the header (eg ah):
and that implements the source file (eg ac):
Code:
# include "ah"
# include <stdio.h>
void foo () (
printf ( "plop! \ n");
)
Suppose now that implement function foo () in ah (ie the feature is not just declared in ah). In other words, the file contains ah
Code:
# include <stdio.h>
void foo () (
printf ( "plop! \ n");
)
... ac and the file contains, for example:
# include "ah"
void f () (
foo ();
)
The file is included by ah main.c and ac Thus ah content is copied to ac and main.c. Thus, these two files each with an implementation of the function foo () (the same course!), But the compiler will not know which make and trigger an error multi definition when linkage:
Code:
(mando @ Aldur) (~) $ gcc-Wall-W main.c ao
ao: In function `plop ':
ac: (. text +0 x0): multiple definition of `plop '
/ tmp / ccmRKAvQ.o: main.c: (. text +0 x0): first defined here
collect2: ld returned 1 exit status
This justifies why the implementation of a function must be generally in a source file (. C or. Cpi) and not in a header (. H. Hpp). It will just two exceptions to this rule in C + +: inline functions and functions templates (or methods of a class template).
Bookmarks