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

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.

Related

Split a header-only library into implementation and header

Pardon my strong wording, but I believe that header files are just as bad as using namespace std;. I want to use a library called CLI11, which is header-only. Is there a way to convert that header-only file into two files, the header file (declarations only), and a .cpp file containing the implementation?
Ideally, something automated, not cut and paste.
Visual assist has a 'Move implementation to source file' routine. I don't think it will handle an entire complex header file or a set of header files.
Perhaps more practically, you can mitigate header file pollution by managing your include directories carefully. CLI parsing, for instance, should only be needed by a single cpp file, so you could set that cpp file up to compile with a special include directory and then only use the library in that cpp file.

Difference in including the .cpp file and .h file (with the same content in cpp)?

I've recently started learning cpp from basics and was very much confused with the folowing:
Lets say I have a header( test.h which contains only declarations) with some content and some source file (source.cpp) and program produced some result.
If I have copied the same content of that header file to a .cpp file (testcpp.cpp) and included this in source.cpp
In this case, I did not understood what difference it makes?
(I'll not include this testcpp.cpp in make file)
I have seen some threads similar to this but couldn't get a clear idea!!!
I learnt the usage of header and cpp files and have used it correctly in projects till now, Please answer specific to this scenario (I know doing this way adds confusion but just want to know). Will there be any difference doing so or it's just a common practice everyone follows ?
what difference it makes?
The extension of a header file has no effect on anything. You could have just as well named the file test.mpg, .test or just test (changing the include directive obviously), and it would have worked just as well. The extension is for the benefit of the programmer, not the toolchain.
However, it is a bad idea to name it anything other than .h, .hpp or whatever is your convention. If you name it .mpg, people will think that it is a video, and not realising that it is a header file, try to play it in a media player. If you name it .cpp, people will think that it is a source file and may attempt to compile it or maybe add definitions into it.
Including a file with the preprocessor is technically just copying contents of one file into another. Nothing more and nothing less. Everything else about them is just convention.
In makefile, when specifying source file, Can I give my source files with any extension(.fsfs, .xxx) rather than .cpp extension
Technically yes, however compilers usually use the source file extension to detect the language which they will fail to do in this case, so you would have to specify it explicitly.
It changes nothing. It's just a convention whether you use a *.h or *.cpp or *.asdasd suffix, as long as it doesn't get compiled by itself.
Some projects use the .hxx extension for header files and .cc for source file.
Please, for the good of fellow programmers you'll work with, stick to common conventions and don't put header code in .cpp files.
#include just does a copy-n-paste of the file you include into the current file. What the file is named doesn't matter one bit - you can name it "foo.exe" if you like; as long as it contains valid source-code in the context where it is included all is well (but please don't use unconventional names, you'll just confuse people).

What does it mean when a C++ file has .cpp.hpp at the end of the filename?

This question has nothing to with why C++ has header files, or what extensions are commonly used for header files.
Why would a file have both ".cpp" and ".hpp" in the name?
For example,
example.cpp.hpp
Sorry if the answer seems obvious or easily searched, but I've looked through a number of different search engines and can't find anything.
Your file is a almost certainly a header file, because of the .hpp extension. (It could be a source file, no one knows, the extension doesn't mean anything).
Note that only .hpp is the extension, not .cpp.hpp. The actual file name is example.cpp. There can only be one extension for any given file.
Why would anyone do that? Here's a theory:
The default option on Windows (I think) is to hide file extensions. The person maybe tried to change the extension, by renaming the file. But because the actual extension was hidden, he added .cpp to the actual filename, and so the file became example.cpp.hpp. Because the .hpp part was hidden, the person thought that the file had the correct extension, when it hadn't.

xcode 4.6 iostream file not found error?

I am facing a problem regarding iostream file not found in header file.I just added a c++ file in my project a header file also included by default with some macro definition and including iostream file as
#ifndef __ObjectiveCPlus__File__
#define __ObjectiveCPlus__File__
#include <iostream>
#endif
but at this line I am getting error at include line as
I google it a lot and found various types of answer regarding this.But no one is able to correct my errors.
Please help
Thanks!
You don't need <iostream> in your header file, put it in your .cpp file. You're not referring to anything in the iostream library in your header file, using this library is more of an implementation detail.
Why?
I believe UIAppDelegate imports UIViewController.h, that includes MathUtils.h. Because UIAppDelegate's implementation is in a .m file, it's being compiled for Objective-C, and this chain of includes (which is all based on the header files) is including something that is C++. As such, the Objective-C portion is unable to find <iostream>, as that library does not exist in pure Obj-C.
Putting it in your .cpp file limits it to one compilation unit, the MathUtils unit. Having it in your header file includes it in all compilation units that have a dependancy on whatever is using it, which may not be Objective C++.
Alternative Solution
You could have your whole project as Objective C++ (in this case, by changing UIAppDelegate.m to UIAppDelegate.mm), which means C++ can be used throughout. I'm not a fan of this method, and it could mask bad coding practices.
I got the solution from another post:
Renaming your implementation file with .mm extension instead of .m will solve the issue.

LLVM refuses to compile C++ source, weird errors

I've made a struct which does cached file manipulation for my application. I built and tested to in a separate project before putting it into my current one.
Ever since I've moved it over, Xcode refuses to build it. Except when I don't include the file from any Objective-C based header file.
I get one error when I try to include iostream:
And more when I comment it out:
Its file extension is .mm, however I have tried it with .cpp and .hpp, but all of them refuse to build unless I don't #include it from the Objective-C header file.
I've also tried #import from iostream and the file itself in the Objective-C header file.
Any clues as to why this is happening?
As a matter of principle, you cannot include a C++ header file from an objective-C source file.
After all, #including (or #importing) a file only means that the preprocessor replaces the #include directive by the contents of the #included file, before passing the result on to the "actual" compiler. The file extension of the header file is a matter of convention, only, it has no actual meaning.
The error messages your are seeing are clearly the result of the file being compiled as [Objective-]C rather than [Objective-]C++.
Solution: All the source files that include your C++ header file have to be either C++ (.cpp or .cc or a few other extensions) or Objective-C++ (.mm). All source files that include a header file that includes your C++ header file, also have to be C++ or Objective-C++.
EDIT: I just saw that you are defining non-inline, non-template functions in your C++ file that you want to include. This is an unrelated problem, but it will lead to "multiple definition" errors sooner or later. Those function definitions belong in a .cpp, which shouldn't get #included anywhere, only the struct/class definition belongs in a header.
Take a look here and here. You need to tell the compiler to include libstdc++. When mixing Objective-C and C++ all you're files need to have the ".mm" extension, as stated in the second link.
I suspect the error is occurring when you compile a .m or .c file that includes the same header.