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

Are redundant include guards necessary?

Are 'redundant include guards' necessary in Codegear RAD Studio 2009? Is the compiler smart enough to deal with this on it's own? For example, I might have the following 'include guard' in foo.h: #ifndef fooH #define fooH // ... declaration…
Seth
  • 8,213
  • 14
  • 71
  • 103
5
votes
1 answer

How can I make Doxygen not document my include guard?

I'm using Doxygen 1.8.13 on a C++ project. Generally, I want Doxygen to document my macros. However, I obviously don't want it documenting my header file include guards: #ifndef FOO_H_ #define FOO_H_ ... etc ... #endif // FOO_H ... but Doxygen, by…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
5
votes
3 answers

How do you define functions in header files?

The setup If I have a program like this A header file that declares my main library function, primary() and defines a short simple helper function, helper(). /* primary_header.h */ #ifndef _PRIMARY_HEADER_H #define _PRIMARY_HEADER_H #include…
RBF06
  • 2,013
  • 2
  • 21
  • 20
5
votes
3 answers

multiple definition and namespace

Is that the right way to have functions in namespace that i will #include in multiple files? test.h #pragma once #ifndef TEST #define TEST namespace test{ namespace { bool test(){ return true; } …
Stals
  • 1,543
  • 4
  • 27
  • 52
5
votes
1 answer

Quick question regarding Conditional Compilation (ifndef)

This is quite probably a very silly question but I need to be sure. I've been given a class declaration in a header file eg. #ifndef file_H #define file_H class ex{ private: public: }; #endif and I've been required to write the method…
silent
  • 2,836
  • 10
  • 47
  • 73
5
votes
4 answers

C++ header guards around object and usage?

I am used to putting header guards around my objects like: #ifndef SOMETHING_H #define SOMETHING_H class Something { ... } #endif but I have been given code where they also do: #ifndef SOMETHING_H #include "something.h" #endif for every include. …
Chad Befus
  • 1,918
  • 1
  • 18
  • 18
5
votes
2 answers

Include Guard still inserting Global Variables

I have 3 *.c files (file1.c, file2.c and file3.c) and 1 *.h file (file3.h) in a project (Visual Studio). /******************************* file3.h ********************************/ #ifndef FILE3_H #define FILE3_H int gintVariable =…
OnlyQuestions
  • 169
  • 1
  • 8
5
votes
2 answers

Does "#pragma once" have the potential to cause errors?

All of my header files use include guards as well as pragma once: #pragma once #ifndef FILE_NAME_H #define FILE_NAME_H class foo { //foo interface.. }; #endif /* FILE_NAME_H */ I understand that pragma once is not standard and may not be the…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
4
votes
2 answers

Does the STL specify the header guards for its headers?

I want to supply some inline convenience functions using std::string in my header alongside with the library functions which use const char *, but I do not want to include . I want to check with #ifdef if is included and provide the…
Fabian
  • 4,001
  • 4
  • 28
  • 59
4
votes
1 answer

How to include functions with auto return type from header file into multiple cpp files

I want to define a function of auto return type in such a way, that I can call it from multiple .cpp files if I include the header. I have 4 files head.hpp - where the function is #ifndef HEAD_HPP #define HEAD_HPP auto f(); #endif head.cpp -…
Bl4ckC4t
  • 137
  • 3
  • 11
4
votes
3 answers

#pragma once vs. include guards

I am going through Implementation defined behavior control and there is the following text in relation to #pragma once: Unlike header guards, this pragma makes it impossible to erroneously use the same macro name in more than one file. I am not…
Vinod
  • 925
  • 8
  • 9
4
votes
2 answers

Qt creator include guard pattern

There's a way in Qt-Creator to automatically add a template string while creating a new class? For instance: I create the new class Foo. The header file auto-generated is #ifndef FOO_H #define FOO_H class Foo{ }; #endif FOO_H What I…
Moia
  • 2,216
  • 1
  • 12
  • 34
4
votes
2 answers

C++ cyclic inclusion issue

I have this file logger.hpp: #ifndef _LOGGER_HPP_ #define _LOGGER_HPP_ #include "event.hpp" // Class definitions class Logger { public: /*! * Constructor */ Logger(); /*! * Destructor */ ~Logger(); /*! …
Andry
  • 16,172
  • 27
  • 138
  • 246
4
votes
4 answers

Auto defines in C editors... Why?

When Eclipse creates a new file (.c or .h file) in a C project the editor always auto creates a #define at the top of the file like this: If the file is named 'myCFile.c' there will be a #define at the start of the file like this #ifndef…
c0m4
  • 4,343
  • 10
  • 35
  • 40
4
votes
0 answers

if it possible to automatically #include guards in Sublime?

I typically work with a lot of header (.h) files in Sublime tex editor and it would be very helpful if there was a way to automate the inclusion of: #ifndef _STUFF_H_ #define _STUFF_H_ //some stuff #endif Does anyone know of a way to automatically…
roulette01
  • 1,984
  • 2
  • 13
  • 26