Libraries are collections of often used procedures. Even more, these procedures can do very difficult tasks for us, which to implement by ourselves would take hours (days,weeks,months....). So, why not to write a example program, which uses a library procdedure, and header files? And with this example we will get an error, which will help us to understand some special context. At first, let's have a closer look to a stripped version of our first program (I have left of the code to detect the kind of compiler, you use...) #include<stdio.h> #include<stdlib.h> int main(int argc, char *argv[] ) { printf( "Hello, Hackers!\n" ); } Do you see, what is special with this one? No? Remember the text above! No clue? "Before using a function, you have to write down a prototype of it." There are two functions used in this program: main() printf() The prototype of "printf" is defined in the header file /usr/include/stdio.h If you give the command grep printf /usr/include/stdio.h You will see a couple of prototypes, not only of printf itself, but of similar functions. What's with main() ? This one is special!!! It is the mother/the father of all functions. It is the point where execution of the program starts. There can be only one function of this name but it must be there. So, to be convenient, the prototype of this special function is directly "burned in" the compiler itself. Let's change the program to use more than main(). Let's put in the use of header files defined by ourselves and a call to a library function. Change the above source code to: #include<stdio.h> #include<stdlib.h> #include "myprocs.h" int main(int argc, char *argv[] ) { showme( "How to hack!" ); } void showme( char *mywish ) { printf( "%s\n", mywish ); printf( "%f\n", sin(35.0)); } Save this as "hh2.c" with your editor. Next use your editor to create a file with the contents: void showme( char *mystring ); Save it as "myprocs.h". Now give the command: cc hh2.c -o hh2 Time to learn! There are errors.... The output of the compiler looks something like : hh2.c: In function `showme': hh2.c:14: warning: type mismatch in implicit declaration for built-in function `sin' /tmp/cca008671.o: In function `showme': /tmp/cca008671.o(.text+0x36): undefined reference to `sin' collect2: ld returned 1 exit status Let's see what the compiler is trying to tell us. First line: The compiler tells us that there is something wrong inside the function showme(). Second line: There is a warning! Be warned! DON'T think: "Oh, this is only a warning, no error, so: proceed!" But a warning is the error of tomorrow. But it is more difficult to find those warning based errors. This warning said, that there is a type mismatch in the implicit call of the built-in function sin. |