Difference Between Using #Include<Filename> and #Include<Filename.H> in C++

Difference between using #includefilename and #includefilename.h in C++

C++ only include-files not found in the C standard never used filename.h . Since the very first C++ Standard came out (1998) they have used filename for their own headers.

Files inherited by the C Standard became cfilename instead of filename.h. The C files inherited used like filename.h are deprecated, but still part of the C++ standard.

The difference is that names not defined as macros in C are found within namespace std:: in cfilename in C++, while names in filename.h are within the global namespace scope. So you will find ::size_t in stddef.h, and std::size_t in cstddef. Both are Standard C++, but use of ::size_t is deprecated (See Annex D of the C++ Standard).

Now those were the difference.

Why would you use `filename.h` ?

  • Compatibility with C compilers
  • Compatibility with very old C++ compilers

Why should you use `cfilename` ?

  • Names are within namespace std:: . No name-clashes anymore.
  • New C++ features (e.g. overloaded math functions for float, long)
  • C Compatibility Headers (filename.h) could disappear in future.

C++: What is the difference between an include directive and a header file?

#include is a pre processor directive. Pre processor directives direct the behaviour of the pre processor. The pre processor processes i.e. modifies the source code prior to compilation.

The pre processor replaces #include directives with the content of the file that is the argument of the directive. The files that are included in this way are called header files because they are typically placed at the beginning (head) of the file. So, #include <iostream> will be replaced by the content of the header file named iostream (except, the standard library headers such as iostream do not necessarilly exist as files).

Inclusion of header files with the directive allows using the same header in the beginning of multiple source files. However, C++ standard disallows multiple definitions of functions and variables (though there are some exceptions) across multiple source files. This is why header files typically only contain declarations of functions and variables, not definitions. It is the declarations that the compiler must know of the functions and variables in order to understand the program the uses them.

What is the difference between an include directive and a header file?

As you seem from my description of both include directive, and header file, they are quite different concepts. Your question is analogous to: "What is the difference between a pen, and a short story". You can write a short story with a pen, but they are not very comparable as they are so different concepts. Similarly, you can include a header file with the include directive, but they are hardly comparable.

What are the differences between #include filename and #inlude filename specific to LLVM/Clang?

I couldn't find anything in the documentation that specifies the expected behavior in each case, so I was curious and decided to look at the code in here. Might be wrong to assume this is how it will always behave, but without the documentation, I guess the source code is the next best thing.

Searching through the code I found that the #include pragma is processed here in the HandlePragmaDependency method (line 453), where it determines if the include isAngled or not (line 468) and then uses that value as a parameter to the LookupFile() method (line 477).

This method is defined in the HeaderSearch.cpp (line 498) and the documentation comment for it states that:

[...] isAngled indicates whether the file reference is for system #include's
or not (i.e. using <> instead of ""). [...]

Later in that method the value for isAngled is used in three places (line 547, 596 and 673) each of which have the following comments.

  // Unless disabled, check to see if the file is in the #includer's
// directory. This cannot be based on CurDir, because each includer could be
// a #include of a subdirectory (#include "foo/bar.h") and a subsequent
// include of "baz.h" should resolve to "whatever/foo/baz.h".
// This search is not done for <> headers.
if (!Includers.empty() && !isAngled && !NoCurDirSearch) {

...

// If this is a system #include, ignore the user #include locs.
unsigned i = isAngled ? AngledDirIdx : 0;

...

// If we are including a file with a quoted include "foo.h" from inside
// a header in a framework that is currently being built, and we couldn't
// resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
// "Foo" is the name of the framework in which the including header was found.
if (!Includers.empty() && !isAngled &&
Filename.find('/') == StringRef::npos) {

Hope it helps.



Related Topics



Leave a reply



Submit