Skip to main content

Section 6.3 Header Files

Separate compilation requires that each .cpp file must have a declaration for every function that will be used in it.Manually declaring each function from a library (like we did with doubleValue in main.cpp) so that you can use it in another file works, but is a bit cumbersome. Imagine that I want to use a dozen different functions from a library - I would have to start my file with a dozen declarations for the various functions.
Fortunately, there is an easier way to add all the function declarations for the functions in a library - by using a header file. A header file is a file that contains a set of function declarations that match what is in a .cpp file or compiled library.
Here is a header file that might correspond to library.cpp:
Listing 6.3.1. library.h
// If we have not yet defined LIBRARY_H, keep going
// Otherwise, skip to the #endif
#ifndef LIBRARY_H
// Define LIBRARY_H so if we end up back in this file we skip over everything
#define LIBRARY_H

// Declare the functions we will be using
/**
 * @brief Doubles the value of the input number
 * 
 * @param num an integer value
 * @return int 2x the input number
 */
int doubleValue(int num);

#endif
Key things to note:
  • It has the same name as the .cpp file, but with a .h extension. This is a convention, not a strict requirement.
  • It has the declarations for the function(s) that exist in the .cpp file, but it does NOT have the definitions.
  • It does not have to declare all the functions that exist in the .cpp. The getMultiplier function is not declared in the header file because it is not intended for use outside of the library.cpp file.
  • The function’s comment goes in the .h file with the declaration. The .h file is what β€œother” programmers should refer to when they want to know what is in library.cpp and how to use it - ideally they should never need to look in the .cpp file.
  • Around the function declarations there is the special #ifndef ... #endif construct (lines 3, 5, 16). These lines ensure that even if we include this file multiple times, it will only be read one time. Line 3 asks β€œHave we not defined LIBRARY_H?” (ifndef). If we have not, we keep going. Otherwise, all the code until the endif is skipped. The first thing we do inside the ifndef is to define LIBRARY_H. That way, if we end up in this file again, it will be clear we have already seen the code.
    The name we check/define can be anything, but by convention, it usually matches the name of the file (in this case LIBRARY_H).
It is possible to have functions defined in library.cpp that are not declared in library.h. Doing so indicates that the unlisted functions are not for use by other .cpp files - they are internal functions that other code should not rely on.
If the declarations in the file header depend on types defined in other files, those files must be included at the top of the header file. For example, to refer to a string, we need to #include <string> at the top of the header file. However, it is bad practice to use using namespace std; in a header file. The header file code may get included into multiple different .cpp files, and putting using namespace std; in the header means that it will be added to each of those files. Although that will likely work fine in a small project, it could lead to naming conflicts in larger ones. So instead, we should use the full formal name std::string instead of the using namespace shortcut:
Listing 6.3.2. otherLibrary.h
// If we have not yet defined OTHERLIBRARY_H, keep going
// Otherwise, skip to the #endif
#ifndef OTHERLIBRARY_H
// Define OTHERLIBRARY_H so if we end up back in this file we skip over everything
#define OTHERLIBRARY_H

// Include necessary headers
#include <string>

// using namespace std;  <-- Intentionally NOT used here
// We do NOT want to use "using namespace std;" in header files
// because it can lead to name conflicts in larger projects.

// Function just for demo purposes...
// We must use std::string because we are not using "using namespace std;"
std::string firstHalf(const std::string& text);

#endif

Checkpoint 6.3.1.

Write a header file mycode.h for a library that just has a void function called print that takes a string parameter. You will not use all of the blocks.
You have attempted of activities on this page.