Questions tagged [include-guards]

Anything related to C/C++ include guards technique, i.e. a technique employing C-preprocessor conditional compilation features in order to prevent multiple inclusion of header files in C/C++ source files.

Anything related to C/C++ include guards technique, i.e. a technique employing C-preprocessor conditional compilation features in order to prevent multiple inclusion of header files in C/C++ source files.

185 questions
9
votes
4 answers

Template classes and include guards in C++

Is it wise to have include guards around template classes? Aren't template classes supposed to be reparsed each time you reference them with a different implementation? N.B In Visual C++ 2008 I get no errors combining the two...
Bill Kotsias
  • 3,258
  • 6
  • 33
  • 60
9
votes
4 answers

C++ Include Guards for Standard Headers

I am wondering if/ what include guards on files like windows.h, math.h, iostream, stdio... etc. Since I have those headers included multiple times in different files. Do those files already have guards built in or is there a definition defined? I am…
Evan Carslake
  • 2,267
  • 15
  • 38
  • 56
9
votes
4 answers

#ifndef syntax for include guards in C++

I'm currently studying for a CS course's final exam and I've run into a minor (maybe major?) issue regarding the syntax of C++ #ifndef. I've looked at the syntax for #infndef when using it as an #include guard, and most on the web seem to…
jayjyli
  • 771
  • 3
  • 11
  • 23
8
votes
6 answers

Header/Include guards don't work?

For some reason, I'm getting multiple declarations of content within my header file even though I'm using header guards. My example code is below: main.c: #include "thing.h" int main(){ printf("%d", increment()); return…
user1007968
  • 83
  • 1
  • 3
8
votes
3 answers

What is proper LLVM header guard style?

In clang tidy, the check [llvm-header-guard] looks for LLVM style header guards, but I can't find any examples of proper LLVM header guard style, specifically the structure of the name given to the define, the coding standards pages does not mention…
A-n-t-h-o-n-y
  • 410
  • 6
  • 14
8
votes
4 answers

Are tokens after #endif legal?

I currently do the following and the compiler (MSVC2008 / as well as 2010) doesn't complain about it but I'm not sure if it's a bad idea or not: #ifndef FOO_H_ #define FOO_H_ // note, FOO_H_ is not a comment: #endif FOO_H_ I used to always write…
Joe.F
  • 165
  • 1
  • 2
  • 6
7
votes
1 answer

#include guard before or after comment block?

I have somewhere read (sorry can't find the link anymore) that on the first line of a header should always be the #include guard, because compilers can see it without opening the header file. So if a header file has already been included, it won't…
Vuks
  • 801
  • 1
  • 7
  • 24
7
votes
1 answer

"Multiple include guards may be useful for" what, exactly?

I've been playing around with the -H option of gcc, which prints out information about direct and indirect includes in C and C++ compilation (relevant section of the gcc manual). As part of the output there's a section "Multiple include guards may…
R.M.
  • 3,461
  • 1
  • 21
  • 41
7
votes
4 answers

in C++ , what's so special about "_MOVE_H"?

I have a C++ file like this #ifndef _MOVE_H #define _MOVE_H class Move { int x, y; public: Move(int initX = 0, int initY = 0) : x(initX), y(initY) {} int getX() { return x; } void setX(int newX) { x = newX; } int getY() { return…
phunehehe
  • 8,618
  • 7
  • 49
  • 79
7
votes
2 answers

Internal vs External Include Guards

I've heard that you should prefer writing internal include guards instead of external include guards. I have searched around the internet but haven't found an answer to it. This is a snippet of the book C++ Coding Standards by Herb & Andrei that…
Andreas DM
  • 10,685
  • 6
  • 35
  • 62
7
votes
3 answers

Protecting one class from the bad programming of another?

Is there a way in PHP to try to include a file, but if the file contains errors that stop it from compiling to just skip that file from inclusion?
Mark Tomlin
  • 8,593
  • 11
  • 57
  • 72
7
votes
5 answers

In C and C++, why is each .h file usually surrounded with #ifndef #define #endif directives?

Why does each .h file starts with #ifndef #define #endif? We can certainly compile the program without those directives.
user133466
  • 3,391
  • 18
  • 63
  • 92
6
votes
3 answers

C++ include guard

So I know how to place an include guard in my own header files with the standard #ifndef ... #define ... Now, My question is about including libraries that are not my own. would be a good example. I have a header file which requires the use of…
grep
  • 3,986
  • 7
  • 45
  • 67
6
votes
1 answer

Doxygen demands that an include-guard be documented

Please do not mind the strangeness of the following minimal example (I would have to make it much larger to justify why I am doing things this way): File test.cpp: #include "a.h" int main() { return 0; } File a.h: namespace N { // without…
AlwaysLearning
  • 7,257
  • 4
  • 33
  • 68
6
votes
3 answers

When to use include guards or #pragma once C++

Is it good practice to use your choice of either/both include guards and #pragma once in every header file, or just those with something such as a class declaration? I am tempted to put it in every header, but I'm afraid it would be unneeded and…
Matthew D. Scholefield
  • 2,977
  • 3
  • 31
  • 42
1 2
3
12 13