Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CPlusPlusNotesForProfessionals.pdf
Скачиваний:
47
Добавлен:
20.05.2023
Размер:
5.11 Mб
Скачать

Chapter 45: Header Files

Section 45.1: Basic Example

The following example will contain a block of code that is meant to be split into several source files, as denoted by

// filename comments.

Source Files

// my_function.h

/* Note how this header contains only a declaration of a function.

*Header functions usually do not define implementations for declarations

*unless code must be further processed at compile time, as in templates.

*/

/* Also, usually header files include preprocessor guards so that every header

*is never included twice.

*The guard is implemented by checking if a header-file unique preprocessor

*token is defined, and only including the header if it hasn't been included

*once before.

*/

#ifndef MY_FUNCTION_H #define MY_FUNCTION_H

//global_value and my_function() will be

//recognized as the same constructs if this header is included by different files. const int global_value = 42;

int my_function();

#endif // MY_FUNCTION_H

// my_function.cpp

/* Note how the corresponding source file for the header includes the interface

*defined in the header so that the compiler is aware of what the source file is

*implementing.

*

*In this case, the source file requires knowledge of the global constant

*global_value only defined in my_function.h. Without inclusion of the header

*file, this source file would not compile.

*/

#include "my_function.h" // or #include "my_function.hpp" int my_function() {

return global_value; // return 42;

}

Header files are then included by other source files that want to use the functionality defined by the header interface, but don't require knowledge of its implementation (thus, reducing code coupling). The following program makes use of the header my_function.h as defined above:

// main.cpp

 

#include <iostream>

// A C++ Standard Library header.

#include "my_function.h"

// A personal header

int main(int argc, char** argv) { std::cout << my_function() << std::endl;

GoalKicker.com – C++ Notes for Professionals

246

return 0;

}

The Compilation Process

Since header files are often part of a compilation process workflow, a typical compilation process making use of the header/source file convention will usually do the following.

Assuming that the header file and source code file is already in the same directory, a programmer would execute the following commands:

g++ -c my_function.cpp

#

Compiles the source file my_function.cpp

 

#

--> object file my_function.o

g++ main.cpp my_function.o # Links the object file containing the

#implementation of int my_function()

#to the compiled, object version of main.cpp

#and then produces the final executable a.out

Alternatively, if one wishes to compile main.cpp to an object file first, and then link only object files together as the final step:

g++ -c my_function.cpp g++ -c main.cpp

g++ main.o my_function.o

Section 45.2: Templates in Header Files

Templates require compile-time generation of code: a templated function, for example, will be e ectively turned into multiple distinct functions once a templated function is parameterized by use in source code.

This means that template function, member function, and class definitions cannot be delegated to a separate source code file, as any code that will use any templated construct requires knowledge of its definition to generally generate any derivative code.

Thus, templated code, if put in headers, must also contain its definition. An example of this is below:

// templated_function.h

template <typename T> T* null_T_pointer() {

T* type_point = NULL; // or, alternatively, nullptr instead of NULL // for C++11 or later

return type_point;

}

GoalKicker.com – C++ Notes for Professionals

247