2

I know that to include headers you must type #include <header.h>.

Is there a way to include the header as #include <header>, like we do with iostream or cstdlib?

6
  • 3
    name the file header.
    – tkausl
    Dec 7, 2017 at 19:45
  • When doing #include <$(something>, $(something) is the name of the file, so just dont give it the .h extension
    – Borgleader
    Dec 7, 2017 at 19:47
  • @tkausl That can get confusing on Windows :-D ...
    – user0042
    Dec 7, 2017 at 19:50
  • 1
    I know that to include headers you must type #include <header.h> that is wrong.
    – user2672107
    Dec 7, 2017 at 19:52
  • 2
    But not only built-in functions do that. Qt libraries are accessed like that, for example.
    – XorTroll
    Dec 7, 2017 at 19:57

1 Answer 1

8

Just rename header.h to header. The files iostream, cstdlib, etc are files with exactly those names that don't have any extension. For example, here is a screenshot of Visual Studio's iostream file in Windows Explorer:

enter image description here

Usually when you create your own header file it has the extension .h (or sometimes .hpp), but nothing in the C++ standard forces them to have any specific extension. You can include a file with any you want as long as its contents is valid C++. Some libraries like the C++ standard library or Qt don't have any extension on their header files just to make your code look nice. Most IDEs automatically add a .h extension when you create a header file because .h is the extension that header files usually have, but that's just a convention, you can give them any extension you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.