Search code examples
c++pragmaprecompiled-headerspreprocessor-directive

Need some clarification on #pragma once


I've searched all over for some clarification on what #pragma once actually does and can't find definitive answers for some questions I still have.

Does #pragma once insure that the header file it is included in is only called once AS WELL AS that the headers which are included in said header file are not yet included? Also, if it is only called once, does that mean a .cpp file that needs a particular header will not be able to access it? If a header file is marked with #pragma once and included in a .cpp, can that header file be used again elsewhere?

These are the sorts of clarifications I am not finding. Sorry if there is documentation that clarifies this somewhere, but I really couldn't find any thing specific enough.


Solution

  • #pragma once only guards a single file in a single translation unit, not counting its sub-hierarchy of inclusion. (However, if the file's second inclusion is prevented, it doesn't have an opportunity to doubly-include anything else.)

    You can still include it again from another .cpp.

    The file is usually identified by its inode number.

    Note that #pragma once is strictly nonstandard, and most still prefer traditional #ifndef header guards.