Add Header and Source Files

Add awesome.h Header to the Project

A simple library needs a header file.  Recall from the Reusable Code Functions tutorial that each function you created needed a forward declaration of its function prototype, to let the C compiler know what it might encounter.   A library's header file collects these function prototypes in once place, so you don't have to add a forward declaration when using a function from the library.  If a library uses functions from other libraries, its header file should also #include those other libraries.

So, let's make a .h file for your awesome library.

  • Click the Project menu and select Add Tab to Project.

  • Click the Save as type dropdown menu at the bottom of the Add Tab dialog and set it to C Header File.
  • Name the file "awesome" and click Save.

Now you have two files open in SimpleIDE: libawesome.c and awesome.h

  • Copy the #include “simpletools.h” and the awesome and epic forward declarations from libawesome.c (first tab) and paste them into awesome.h (second tab) as shown below.
  • Click Project > Save to make sure your changes to the .h file are saved. You will notice the little asterisk on the file tab go away indicating your work is saved. If you make changes, the asterisk will come back as a reminder.

Why #include simpletools.h? 
The awesome and epic functions both use the print function. The print function is part of the simpletools library, so we need to #include it in the awesome library.


Add awesome.c to the Project

Simple libraries tend to have one or more C language source files.  This is where the complete function definitions and their code blocks go.  This file also has to #include awesome.h. 

  • Click the Project menu and select Add Tab to Project (again).

  • This time, you can accept the default Save as type setting –C File (*.c).
  • We’ll use awesome.c, but unlike the previous names, this one does not have to fit any particular pattern dictated by the library name.

  • Add #include “awesome.h” to the beginning of awesome.c.
  • Copy the awesome and epic functions from libawesome.c (leftmost tab) and paste them into awesome.c (third tab).
  • Save the project again.


Clean up libawesome Library Test Code

Now that the library has its header file with forward declarations and its source file with functions, the libawesome application doesn’t need them.  The libawesome application is still important though. You will use it to build your library's output, and to test the functions to make sure they work as expected.  The library's application file is sometimes called a test harness.

  • Click the leftmost libawesome.c tab, and delete the awesome and epic forward declarations (above the main function).
  • Also delete the awesome and epic functions (below the main function).
  • When you are done, your code should look like this:

  • Click Run with Terminal and verify that it still runs.