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
422
votes
15 answers

Is #pragma once a safe include guard?

I've read that there is some compiler optimization when using #pragma once which can result in faster compilation. I recognize that is non-standard, and thus could pose a cross-platform compatibility issue. Is this something that is supported by…
Ryan Emerle
  • 15,461
  • 8
  • 52
  • 69
226
votes
4 answers

Creating your own header file in C

Can anyone explain how to create a header file in C with a simple example from beginning to end.
Anuragdeb3
  • 2,299
  • 3
  • 14
  • 4
183
votes
33 answers

Prevent direct access to a php include file

I have a php file which I will be using as exclusively as an include. Therefore I would like to throw an error instead of executing it when it's accessed directly by typing in the URL instead of being included. Basically I need to do a check as…
Alterlife
  • 6,557
  • 7
  • 36
  • 49
82
votes
3 answers

Why aren't my include guards preventing recursive inclusion and multiple symbol definitions?

Two common questions about include guards: FIRST QUESTION: Why aren't include guards protecting my header files from mutual, recursive inclusion? I keep getting errors about non-existing symbols which are obviously there or even weirder syntax…
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
60
votes
9 answers

Why isn't C/C++'s "#pragma once" an ISO standard?

I am currently working on a big project and maintaining all those include guards makes me crazy! Writing it by hand is frustrating waste of time. Although many editors can generate include guards this doesn't help much: Editor generates guard…
mip
  • 8,355
  • 6
  • 53
  • 72
53
votes
2 answers

difference between "ifndef" and "if !defined" in C?

I have seen #ifndef ABC and #if !defined (ABC) in the same C source file. Is there subtle difference between them? (If it is a matter of style, why would someone use them in the same file)
onemach
  • 4,265
  • 6
  • 34
  • 52
53
votes
8 answers

C++ #include guards

I have three classes, GameEvents, Physics and GameObject. I have headers for each of them. GameEvents has one Physics and a list of GameObjects. Physics has a list of GameObjects. I'm trying to achieve that GameObject is able to access or own a…
Orujimaru
  • 835
  • 2
  • 13
  • 18
39
votes
5 answers

Header guards in C++ and C

At LearnCpp.com | 1.10 — A first look at the preprocessor. Under Header guards, there are those code snippets: add.h: #include "mymath.h" int add(int x, int y); subtract.h: #include "mymath.h" int subtract(int x, int y); main.cpp: #include…
Simplicity
  • 47,404
  • 98
  • 256
  • 385
35
votes
3 answers

Should I still use #include guards AND #pragma once?

http://en.wikipedia.org/wiki/Pragma_once Should I still use include guards when all of these compilers support #pragma once? A lot of responses on stack overflow say to use both for compatibility, but I'm not sure if that still rings true. What…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
28
votes
4 answers

Should variable definition be in header files?

My very basic knowledge of C and compilation process has gone rusty lately. I was trying to figure out answer to the following question but I could not connect compilation, link and pre-processing phase basics. A quick search on the Google did not…
Methos
  • 13,608
  • 11
  • 46
  • 49
27
votes
9 answers

Naming Include Guards

How are C++ include guards typically named? I tend to see this a lot: #ifndef FOO_H #define FOO_H // ... #endif However, I don't think that's very intuitive. Without seeing the file name it's difficult to tell what FOO_H is there for and what…
Maxpm
  • 24,113
  • 33
  • 111
  • 170
26
votes
3 answers

What exactly do C include guards do?

Let's say I have a header file "header.h" with a function definition. #ifndef HEADER_FILE #define HEADER_FILE int two(void){ return 2; } #endif This header file has an include guard. However, I'm kind of confused as to what #define HEADER_FILE is…
Izzo
  • 4,461
  • 13
  • 45
  • 82
24
votes
3 answers

Customizing inclusion guards in eclipse CDT

Is there a way to customize the format of inclusion guards in eclipse CDT for the class generation template? The current format is _H, but what I would like is something like ___H. Not that I expect to…
cheshirekow
  • 4,797
  • 6
  • 43
  • 47
22
votes
1 answer

How to make CLion use "#pragma once" instead of "ifndef ... def ..." by default for new header files?

By default, CLion will add the following lines to a newly created header file: #ifndef SOME_NAME_H #define SOME_NAME_H .... your code here #endif //SOME_NAME_H But I like #pragma once more. How can I configure CLion so that it uses #pragma once by…
a06e
  • 18,594
  • 33
  • 93
  • 169
19
votes
4 answers

When not to use include guard in header file?

We all know when to use include guard, but when shall we not use it in our project? Recently, I saw a project with mix compilation (CUDA + GCC), one header file (CUDA file) is deliberately left without include guard. I am just curious about it.
Lonelydove
  • 193
  • 1
  • 5
1
2 3
12 13