By now you already have quite a collection of little C programs and it is more than likely that this will continue to grow.
If you have followed all the proceedures during the exercises, all your C programs should be in the directory cprograms. If they are somewhere else, move them to this location a.s.a.p.
Still, with an increasing number of codes, this will become quite confusing. Therefore we should create subdirectories of cprograms to get the software organized. One way to do it is to create a subdirectory for every single program, another is to sort the programs by groups:
Suggestion:
|- demo01 cprograms ------|- demo02 |- demo03 |- demo04 |- freefall |- libraries
Until now you were used to work with programs with a structure such as
main()
function1(...)
...
functionn(...)
You might have already realized that this can cause quite some trouble during compilation - for example, a missing ";" in function2 can cause an error message for the main module, etc.
To make life easier we will introduce the following rule:
Every program module is stored in its own file !
Instead of having just one file, e.g. "example.c" now and in the future we will deal with a list of files, such as exam.c (including only the main module), function1.c, ... functionn.c.
And, instead of using the
cc -o example example.c -lm
we will do the following sequence:
cc -c exam.c
cc -c function1.c
...
cc -c functionn.c
cc -o exam exam.o function1.o ....functionn.o -lm
or
cc -o exam exam.c function1.o ....functionn.o -lm
Don't worry, there are shortcuts.
The sequence
cc -c f*.c
ar -qcv flib.a *.o
cc -o exam exam.c flib.a -lm
will lead to the same result.
ar ist the unix command for archive. It creates a library that in this particular case contains the objects of your functions. The compiler then will use this library during the linking process.
Look up the ar command in the man pages!
Let's have a look to another tool: the make command (<-- This is actually a link)
drago@scri.fsu.edu
Jan. 1997