简体   繁体   中英

C++ Including Files

A problem I'm having is when using a class (one header, one cpp file), I am wondering when including a file in the header file that is also needed in the source file of the class what should I do? I'll give an example

header.h (file)

#include <Windows.h>
#include "some_other_header.h"

class class_name
{
    public:
        LRESULT CALLBACK FUNC(HWND, UINT, WPARAM, LPARAM);
    private:
}

source.cpp (file)

#include "header.h"

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM)

As you know LRESULT Is part of the library and it does not need to be included in "source.cpp" because it's included in the "header.h" file. The problem is if I include "header.h" in any other file I wouldn't have to include either because it's already in "header.h" this is a bit confusing, it's kind of hard to explain but I want to completely avoid any errors this may bring.

It is not necessary. #include directive copies the content of file verbatim in the place of #include . So any file included in a file will be included down. In your example, you have Windows.h> , "some_other_header.h and header.h all included in your .cpp file (as well as any header included by Windows.h and some_other_header.h ).

It is often a good practice to include them anyway, so that you don't depend on some header 10 files above, but it is usually omitted within header-source pair. It's not really resonable to copy the headers if you need them both for function declaration and definition.


On another side, you should add a header guard because of the mechanism mentioned above. Without a guard, you may encounter double definition problems, when a header get's included from different sources. If you are going to use this header in more than one file, they are a necessity, and good practice otherwise.

Standard header guard looks like this:

#ifndef HEADER_H //or any other name, typically it's filename or filename with path
#define HEADER_H 
//your header here
#endif

Most compilers also support non-standard pragma directiveL

#pragma once
//your header here

The good practice is too keep all the files self-sufficient . That is each file should explicitly include headers declaring each symbol that is used in this file. Keeping complete list of includes in each file may seem like a waste of time however not following this rule leads to fragile codebases where modifying includes in one file may trigger a tsunami of broken declarations throughout entire project and drastic time losses.

It's safe to not include the header in your .cpp file as long as it's included elsewhere. #include is almost the same as simply cutting and pasting the file's contents in where the directive is, so after preprocessing, you end up with one huge file that contains everything that got included. As long as it ends up in there before anything that uses its contents, you're good.

I'd suggest, though, that if you use stuff from windows.h in some way that's independent of header.h , you include the header anyway, so you know for certain that you will always end up including the header you need. Any self-respecting library header file, including windows.h , is safe to #include twice -- it keeps itself from being re-included in the same translation unit, whether via include guards or via #pragma once . And if header.h can one day change to not need windows.h , you want to be able to make that change without causing a bunch of errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM