Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing .h extension in user defined c++ header file

Tags:

c++

enter image description hereCan we remove .h extensions while we define our own header file in c++? like in case of standard header files in c++.

I have created a header file and named it add.h and tried including it using #include "add" but it didn't work.

after following up the comments and answers: using codeblocks ide i have created a "add" of type File and tried it including in my source file and it worked. attaching the snapshot below. the aim of my question is to ask if userdefined header files can also omit .h extensions and how? i am really trying to explore this fact and don't have a good understanding of how compilers stores standard header files. A easy to understood conclusion is really appreciated Thankyou.

like image 796
PARUL Avatar asked Jun 01 '20 10:06

PARUL


People also ask

What is .h in header file in C?

A header file is a file with extension . h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.

What is the extension of C header file?

In C language, header files contain the set of predefined standard library functions. You request to use a header file in your program by including it with the C preprocessing directive “#include”. All the header file have a '. h' an extension.

Why .h is removed in C++?

h is deprecated and not a standard header. It was used in older programs before C++ was standardized, Functions like cout were defined inside iostream. h . After C++ got standardized, all these functions like cout got moved into the std namespace.To adjust to this change, non .

Why .h is used in header file?

In C language, header files contain the set of predefined standard library functions. The “#include” preprocessing directive is used to include the header files with “. h” extension in the program.


2 Answers

Can we remove .h extensions while we define our own header file in c++?

Sure, as long as that matches the filename of the file. As far as the language is concerned, the name of the file is largely irrelevant.

However, .h or similar such as .hpp is conventional, and helps the reader of the source to understand what the file is used for. This is an important consideration.

Another consideration is that some tools use the filename as a heuristic to determine the purpose of the file. For example, your IDE might not assume that the file contains C++ code, and thus not enable C++ features such as source analysis unless you follow a common naming convention.

I have created a header file and named it add.h and tried it including in source file using #include "add" but it didn't work.i know i am missing some important concepts here.

What you're missing is that the include directive must match the name of the file. If you include "add", then you must name the file add, not add.h. If you name a file add.h, then you must include "add.h", not "add".

like image 157
eerorika Avatar answered Sep 24 '22 00:09

eerorika


Can we remove .h extensions while we define our own header file in c++? like in case of standard header files in c++.

You've misunderstood how the files in the stardard library are named. The header file iostream is actually named iostream and not iostream.hpp or iostream.h (unless you use a very old compiler).

I have created a header file and named it add.h and tried including it using #include "add" but it didn't work.

The reason that doesn't work is because the pre-compiler tries to read the file add and you've named the file add.h.

like image 32
Ted Lyngmo Avatar answered Sep 21 '22 00:09

Ted Lyngmo