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
2
votes
4 answers

When to use include guards?

I know that the use of include guards in header files is to prevent something from being defined twice. Using this code sample though, was completely fine: foo.c #include #include #include "bar.h" int main() { printf("%d",…
ericw31415
  • 415
  • 6
  • 17
2
votes
1 answer

generate c++ include header guards in atom?

I'm looking for an atom package that generates C++ include guards in .h/.hpp header files. The package should do something like taking an existing C++ header file and surround its contents with appropriate #ifndef #define #endif lines. If the plugin…
Kalsan
  • 822
  • 1
  • 8
  • 19
2
votes
2 answers

Error: redefinition of class [class name]

I have seen this error a lot, and there're a lot of questions about it here, but now I really don't know what to do. User.h #ifndef USER_H #define USER_H #endif // USER_H #include using namespace std; class User { private: struct…
Chisté
  • 39
  • 6
2
votes
3 answers

C++ Library Inclusion Guards

Just a question of style, or possibly even a malpractice I am unaware of. I am currently writing my first piece of software, which will be used and reviewed by people other than myself. When I am writing my code and calling my headers, is it bad…
Tom N
  • 104
  • 12
2
votes
1 answer

ifndef include guard in Gnu Make breaks on nested conditional

I’m trying to implement include guards in Gnu Make. In this Makefile, the first inclusion is OK, while the second one fails with an error. ifndef INCLUDED INCLUDED = 1 $(info Including) define macro ifneq ($(1),) define inner_macro macro…
Palec
  • 12,743
  • 8
  • 69
  • 138
2
votes
2 answers

make error: multiple definitions of... despite include guard

My compiler is giving me some very strange errors about multiple definitions of a function (or rather all functions in a specific .c file). I am using include guards to prevent multiple declarations My header files contain NO definitions, only…
2
votes
6 answers

C++ Header Guard issues

I am making a small C++ framework, which contains many .h and .cpp. I have created a general include which include all my .h file such as: framework.h #include "A.h" #include "B.h" #include "C.h" each .h header are protected with include guard…
JP.
  • 495
  • 1
  • 9
  • 20
2
votes
2 answers

Is there a good way to prevent jQuery from being redefined?

I encountered a problem that took me some time to debug where a plug-in that I was using for jQuery (in this case jFeed) was not working. The problem ended up being because we also used Amazon Associates product previews. The product previews code…
Bernard Chen
  • 6,437
  • 5
  • 23
  • 27
2
votes
2 answers

Purpose of include guards in .c file

I have been seeing code like this usually in the start of source files in C #ifndef _INCLUDE_GUARDS_C #define _INCLUDE_GUARDS_C main() { } function1() { } #endif function2() { } I am confused about the purpose of this ..? I am aware if the…
user2798118
  • 385
  • 1
  • 2
  • 16
2
votes
4 answers

Only include headers in interface, not in implementation

My header files look like: // A.hpp --- A's interface #ifndef MY_H #define MY_H #include #include class A { public: A(const std::string& name); std::vector foo() const; private: std::string m_name; }; #endif And…
Escualo
  • 40,844
  • 23
  • 87
  • 135
2
votes
1 answer

Is #import part of the C++11 standard?

I'm trying to find out whether it's safe to use #import (instead of #include with include guards) in a cross-platform C++11 library? The articles I found on SO and elsewhere seem to indicate that #import was proposed to be included in the C++11…
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
2
votes
2 answers

C kernel library includes and bloat

I am new, so I appreciate your help and patience in advance. I have written a program in C like this: main.c arpsend.h - header w/include guard for arpsend.c functions arpsend.c - includes , , etc. arprec.h - header w/include…
2
votes
0 answers

Any reason to define a header-guard to 1?

Occasionally I observe such header guards: #ifndef FOO #define FOO 1 ... #endif // FOO But I learned it this way: #ifndef FOO #define FOO ... #endif // FOO Is the define to 1 an old relict to some compiler?, does it do any harm nowadays?…
math
  • 8,514
  • 10
  • 53
  • 61
1
vote
0 answers

How to disable inclusion of header when using `#pragma once` (similiary to when using include guards)?

From time to time I have to retrofit unit testing into an unfriendly environment. In these cases include guards are pretty useful, because: I could suppress inclusion of unwanted header file, I could check whether specific file was (not)included…
uuu777
  • 765
  • 4
  • 21
1
vote
2 answers

code guards fail and template from string literal

I know the only way to pass a string literal as template argument is to declare it before: file a.h #ifndef A_H #define A_H #include char EL[] = "el"; template struct myclass { std::string get_name() { return name;…
Ruggero Turra
  • 16,929
  • 16
  • 85
  • 141