Structuring a Code Base
You'll continue to work on the tool to manipulate the star database. So far, you have only a single C source file that contains all the functions and data types. This is bad coding style since it does not allow to reuse code.
Restructuring a Code Base
Restructure your code base into multiple header and C source files:
- Create a new directory
include
that will contain header files from now on. - Create a new directory
src
that will contain all C source files from now on. - Create a new header file
star.h
, which contains:- The type definitions related to the star structure.
- The declarations of functions that allow a user to initialize and print a star structure.
- Don't forget to mark relevant declarations with
extern
. - Create a related
star.c
file with the definitions of the functions.
- Create new files
star-database.h
andstar-database.c
, which regroup all operations related to the handling of the star database/CSV files. - Create new files
star-list.h
andstar-list.c
, which regroup all operations related to lists. - Make sure that each header file is protected against issues due to double-inclusion (see the lecture).
- For each new C source file make sure to include its related header file before all other include files.
- This allows you to detect any missing
#include
preprocessor directives in the header files.
- This allows you to detect any missing
- Clean up the file
stars.c
in order to only include themain
function along with the necessary include directives.- You may remove the test functions from the first exercises.
- Move the
splitPrefix
function into a suitable C source file and give it static linkage.
- Replace all constants related to the size of constellations and star names by references to preprocessor macros. Add corresponding
#define
directives to header files where appropriate.
Make sure that the code compiles without any errors/warnings and verify that your program produces the same output as before. Don't forget the -I
option for the compiler.
Building a Library
The code of the various C source files in your src
directory now are clearly reusable. It thus would make sens to build a library from them ... This is precisely your next task.
- Change into the
src
directory (cd src
). - Compile the C source files
star.c
,star-database.c
, andstar-list.c
individually using, in addition to the usual options, the option-c
. Also, be careful with the-I
compiler option. - Use the
ar
command to build a library from them. Check the lecture slides or the manual page (man page) of thear
command for additional details on the usage of the command. Chose a suitable name for the library -- while respecting the convention that library filenames start withlib
. - Compile the file
stars.c
using your library. Don't forget to supply the-l
and-L
options to the compiler , in addition to the usual options.
Make sure that the compiler does not signal an errors/warnings and verify that the code still produces the expected output.
Checking Memory Usage
Last but not least, verify that your program does not contain any errors related to memory management (invalid pointers, uninitialized variables, memory leaks). For this run your program with the Memcheck
tool from valgrind
. The usage of the valgrind
tool is explained in the lecture slides.
Correct any issues that valgrind
might signal, recompile, recheck, and verify the output as long as your program has memory issues.