Sometimes it’s useful to group a few functions that always work together in the same file and let them share variables that are global to that file.
Let’s say you want your awesome function to track how many times it has been called in your application. And, you want a function named awesome_getCount to report the number of times awesome was called. Here’s one way to make those changes to awesome.c:
Now, each time the awesome function gets called, it adds 1 to a variable named calls. Your application will also be able to call the awesome_getCount function to find out how many times the awesome function was called.
The static modifier makes it so that only functions within the same file can see that variable. There are advantages to making your library file variables static; for instance, you can decide to add a static variable named calls to epic.c using similar code, and not have to worry that functions of one file could read the calls variable from another. The awesome.c static calls variable would only keep track of the number of times awesome is called, and the epic.c static calls variable would only keep track of the number of times epic was called.
This modified library isn’t quite ready to use yet. Since we added a function to the library, we need to make sure to update the header file with an awesome_getCount function prototype.
Make sure to update your tests harness code in libawesome.c to exercise this new function and make sure it works.
If the test harness code works correctly, you can build the library for the memory models the library is designed to support.