Each object (variable or function) must FIRST be DEFINED or DECLARED BEFORE it can be USED |
Obviously, you don't what to type this in each time.
The header file will contain the function declarations along with other parameter definitions and possible variable declarations (yes, remember that you also need to declare variables in C++ before you can use them ?)
The global variables are defined outside any function - they will be discussed later: see click here
Function declarations are sometimes called "function prototypes".
There are 2 ways to organize your functions:
The typical name would be /home/username/lib
We have seen this before. Suppose you have a number of functions in the file myLibFile1.C, you can pre-compile this file using:
CC -c myLibFile1.C
#include <SomeHeaderFile> .... Your program... |
Typically, you would use:
CC -c -I/home/username/.../.../lib myFile1.C |
Make sure you use the FULL pathname
CC -o myProg myFile1.o myFile2.o ..... \ /home/username/.../.../lib/myLibFile1.o \ /home/username/.../.../lib/myLibFile2.o \ .... /home/username/.../.../lib/myLibFileN.o |
NOTE: f(x) does not use any library functions, so it does not need the -I option !
You can use any directory name here....
Again, the typical name would be /home/username/lib
Example:
CC -c myLibFile1.C
ar -rv libMyLib.a myLibFile1.o myLibFile2.o ... |
In other words: write a header file that contains declarations in all the files (myFile1.o, myFile2.o, etc) that you used to create the library file.
#include <SomeHeaderFile> .... Your program... |
Typically, you would use:
CC -c -I/home/username/.../.../lib myFile1.C |
Make sure you use the FULL pathname
CC -o myProg \ (This renames output a.out) -L/home/username/.../.../lib \ (This adds to library path) myFile1.o myFile2.o ..... \ (These are the object files) -lMyLib (This is your library file) |
Normally, you would move the library file "libMyLib.a" to /home/username/lib, but since we built it in this directory, there is no need to move it...
The difference between this MAIN function and the one shown before is the different header file included...
NOTE: f(x) does not use any library functions, so it does not need the -I option !