-1

I have gotten to a point in my course where they use Code::Blocks to automatically make a header file. However, I don't think it's necessary to download Code::Blocks specifically for this reason, and so I am looking for a way to make my own header files.

And so, I started my search online to find out exactly how to do this. I checked How to create my own header file in c++? [closed] and also cplusplus.com

However, the former uses uses #ifndef YOUR_NAME_INCLUDE and #define YOUR_NAME_INCLUDE whereas the latter uses #ifndef __X_H_INCLUDED__ and #define __X_H_INCLUDED__, with 2 underscores surrounding the #ifndef and #define. How should I format my header guards? Does it really matter?

For example, if I have a file foo.h, which looks like this:

#ifndef ???
#define ???

int sum(int a, int b) {
    return a + b;
}

#endif

should I put SUM_INCLUDED, FOO_INCLUDED, FOO_H instead of the ??? or something else altogether?

Second, is making a header file really as easy as just sticking a .h or .hpp to the end of it?

VSCloud9
  • 75
  • 11
  • 2
    This is a good question, but I'm not sure if it fits the StackOverflow model that well. Regardless, yes, header files are just any file which is `#include`d in another file, so it could be a `.h`, a `.hpp`, a `.hxx`, a `.h++`, a `.hh`, a `.helloworld` or whatever you like. I personally use `.hpp` (I don't like `.h`, because `.hpp` gives my editor a hint that the file is for C++ rather than C). Then, for the header guards (the `#ifndef` + `#define` part), it's just whatever you want to do. I would personally recommend `FOO_H` or `FOO_INCLUDED`. – Justin Mar 13 '21 at 22:38
  • 3
    It doesn't matter what exactly you write here, as long as each header has a unique string. Though the standard says that names containing `__` are reserved, so you shouldn't use it. You're also missing `#endif` at the end of file. – HolyBlackCat Mar 13 '21 at 22:39
  • Oh my lord the first plain english explain-like-I'm-five answer I've seen all day! Thank you @Justin – VSCloud9 Mar 13 '21 at 22:40
  • @HolyBlackCat oops. Lemme fix that. – VSCloud9 Mar 13 '21 at 22:40
  • 1
    Aside from the "header guards" discussed above, the "How do I write a header?" approach is you generally code until your source file becomes to big to comfortably scroll through. Then you look for like/related functions to move to a separate source file to reduce the size of the current file. To include the functions you move back in the current file, you create a header that normally contains the function prototypes for each moved function, along with any constants or structs you need to expose. Wrap that info in the header-guards and you have a header for the moved functions. – David C. Rankin Mar 13 '21 at 22:47
  • If you put the function body inside the .h file as you've done here, then the function should be marked `inline`, otherwise you'll get linker errors if you include this in more than one file. – interjay Mar 13 '21 at 22:48
  • Does this answer your question? [How to create my own header file in c++?](https://stackoverflow.com/questions/20015656/how-to-create-my-own-header-file-in-c) – TruVortex_07 Mar 13 '21 at 22:55
  • @TruVortex_07 probably the biggest reason I asked this question is because that question itself caused confusion for me, as the accepted answer doesn't use underscores. – VSCloud9 Mar 13 '21 at 22:57

1 Answers1

2
 #ifndef FOO
 #define FOO

 struct foo {};

 #endif

The above canonical example ensures that the contents (between #ifndef FOO and #define FOO) are compiled only once allowing us to include this file (say foo.h) in multiple compilation units without causing struct foo to be multiply defined.

There really isn't any "magic" about an include file other than this. In fact, the extension doesn't even matter. It could be foo.h or foo.hh or just foo (like the standard library headers like vector or iostream).

Ultimately, it's personal preference how you structure your include guards. Some use __FOO__ (note this runs afoul of C++ standard regarding reserved identifiers) or FOO_H_INCLUDED (which is standard compliant) while others may elect for different patterns.

While it's not strictly standard C++, my team uses #pragma once and foregoes the ifndef/define/endif code. It's supported by all the compilers we use (gcc/msvc/clang) and you don't have drawbacks such as too different foo.h files causing one (or the other) definition to be excluded.

The same file then looks like this

#pragma once
struct foo {};
Chad
  • 18,706
  • 4
  • 46
  • 63
  • 1
    "Ultimately, it's personal preference how you structure your include guards." This is misleading, since the C++ standard reserves a (large) set of identifiers for use by the implementation, and ANY code that uses reserved identifier has undefined behaviour. "Some use `__FOO__` others may elect for different patterns." Anyone who does this introduced undefined behaviour, since *all* identifiers that begin with an underscore that is followed by either a second underscore or an uppercase letter are reserved. – Peter Mar 13 '21 at 23:27
  • Yeah, edited to include that detail though many editors follow this non-compliant pattern by default – Chad Mar 14 '21 at 16:46