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
3
votes
5 answers

C language, proper usage of include guards

I'm trying to create a header file (that will include functions I wrote for AVL Trees) but I am having a slight problem and misunderstanding about the syntax of include guards. Right now my code looks like this #ifndef STDIO_H #define…
Oria Gruber
  • 1,513
  • 2
  • 22
  • 44
3
votes
1 answer

How do I recover from a failed import in Nim?

In Nim I can write the following code to import an external module: import myFancyPantsModule ... # And here I'd use the fancyPants proc This works fine as long as I have the module, but for people who might download the code and not have the…
Grzegorz Adam Hankiewicz
  • 7,349
  • 1
  • 36
  • 78
3
votes
5 answers

Why does global variables in a header file cause link error?

I always get the following error, even if I have put include guard in header file. duplicate symbol _Bittorrent in: …
inherithandle
  • 2,614
  • 4
  • 31
  • 53
3
votes
2 answers

Adding an include guard breaks the build

I added #ifndef..#define..#endif to a file of my project and the compiler fails. As soon as I remove it or put any other name in the define it compiles fine. What could be the problem? Sounds like the file is already declared, but I do not know…
yan bellavance
  • 4,710
  • 20
  • 62
  • 93
3
votes
2 answers

SAS macro include guards

In other programming languages such as C++, include guards are used to prevent multiple inclusions of the same code. Like this in C++: #ifndef FOO_INCLUDED #define FOO_INCLUDED .... #endif Does it make sense to build inclusion guards into your SAS…
Martin Bøgelund
  • 1,681
  • 1
  • 17
  • 26
2
votes
1 answer

expected unqualified-id before ‘-’ token

I ran into a weird situation. My understanding is that the order of include header files doesn't matter much if I have put #ifndef #define #endif flag to all .h files. Old codes a.h #ifndef A_H #define A_H blah blah blah #endif a.cc #include…
Christopher
  • 253
  • 1
  • 4
  • 8
2
votes
1 answer

How do I prevent source() R code being included more than once?

I have lots of R source files. For example, in both A.R and C.R files, B.R is loaded via source(). Now I'd like to use the functions in both A.R and C.R, how can I avoid sourcing B.R repeatedly? Is there any include guard mechanisms similar in…
RNA
  • 146,987
  • 15
  • 52
  • 70
2
votes
5 answers

Confusion with including header files

When I include a header file, lets say, //myheader.h #ifndef MY_HEADER_H #define MY_HEADER_H //.... #endif into, //mycpp1.cpp #include "myheader.h" What I'm told is, when mycpp1.cpp includes myheader.h, MY_HEADER_H gets defined so any attempt to…
user103214
  • 3,478
  • 6
  • 26
  • 37
2
votes
1 answer

Compilation speed improvements include guards vs. precompiled headers

I want to reduce compile time on a large project. Our primary compiler is Visual Studio 2010 but some of the code gets compiled in gcc. We are currently planning to ensure that all our .h files have both include guards as well as #pragma once,…
2
votes
2 answers

Use of #error directive in include guards

I have just stumbled upon a project in which the include guards of every header file look like this: #ifndef A_H #define A_H /* Code */ #else #error "Multiple inclusion of file a.h" #endif Is there an actual usecase for having an #error directive…
sephiroth
  • 896
  • 12
  • 22
2
votes
3 answers

Multiple definitions when using #ifdef

I am having a problem when compiling: Multiple definitions of "myFunction()" I will greatly simplify the problem here. Basically, I have 3 files: "main", "header", and "myLibrary". main.cpp #include "header.hpp" int main() { …
Anselmo GPP
  • 425
  • 2
  • 21
2
votes
2 answers

What does the name _headerfile_h mean

I have been reading Zed Shaw's "Learn C The Hard Way". In the 20th chapter, the author creates a header file e.g. headerfile.h and includes the line #ifndef _headerfile_h. I understand the #ifndef directive but not _headerfile_h. Please explain…
2
votes
4 answers

Should the include guards be unique even between namespaces?

I am using same class name in two namespaces, say A and B. Should the include guards be unique while declaring the classes with different namespaces too? I mean can't there be two files names AFile.h (in different directories) with same include…
Sulla
  • 7,631
  • 9
  • 45
  • 71
2
votes
5 answers

Multiple inclusion in multiple files

I am making a small game. In BattleRecord.h: #ifndef _CHARACTER_H_ #define _CHARACTER_H_ #include "Character.h" #endif class BattleRecord { public: Character Attacker; Character Defender; Status status; int DamageDealt; int…
Amumu
  • 17,924
  • 31
  • 84
  • 131
2
votes
3 answers

Names used for include guards?

Are there any guidelines people tend to follow when selecting a name for their include guard? I don't understand why the name for the .h file will slightly differ from the name used in the include guard. For example, I have seen sphere.h and then…
prr
  • 33
  • 5