Skip to main content

14.4) Consequences


In the previous example, main.cpp will need to call functions defined in data.cpp and stats.cpp. When compiling main.cpp, the compiler will therefore need to see prototypes of those functions:

void read_data(std::ifstream&, std::vector<double>&);
double mean(const std::vector<double>&);
double stdev(const std::vector<double>&);

These prototypes can be defined in one or more header files, which main.cpp can access by means of #include directives. For example, if the three prototypes were defined in data.hpp, then main.cpp would need to look like this:

#include "data.hpp"
int main(int argc, char* argv[])
{
  ...
}

Note the use of quotes in the #include directive rather than angle brackets. When angle brackets are used, the GNU C++ compiler will search certain standard locations for the header; moreover, the header name need not correspond to an actual file on disk. When quotes are used, the compiler will search first in the current directory for the header before looking in other places, and it will also expect the name enclosed in quotes to be an actual filename.