简体   繁体   中英

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

在此处输入图像描述 Can 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.

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" .

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 .

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