Maik Klein
Maik Klein

Reputation: 16148

Header guards for bigger projects

I understand what a header guard is but I never saw how it is used in bigger projects. I am currently writing an OpenGL abstraction layer and I mostly need to include the same files.

So my first naive approach was to do something like this:

#ifndef GUARD_H
#define GUARD_H

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

#include <GL/glfw3.h>

#include <glload/gl_core.h>
#include <glload/gll.h>

#endif // GUARD_H

So I can just do #include "guard.h". But I realized that this is not a really good solution because what if I want to add a simple include?

Yes I probably could write all my includes in this file but I am also not sure if this is a good idea either.

How would you recommend me to structure my header guards? Can you recommend me any resources?

Update 1: small example

test.h

        #ifndef TEST_H
        #define TEST_H

        #include <glm/glm.hpp>
        class test{
        };

        #endif

test1.h
            #ifndef TEST1_H
            #define TEST1_H

            #include <glm/glm.hpp>
            class test1{
            };

        #endif

now I included glm in my test class. but what if I want to do something like this.

#include "test.h"
#include "test1.h"
int main(){
//...
}

Don't I include #include <glm/glm.hpp> 2 times in main?

Upvotes: 1

Views: 169

Answers (2)

JBL
JBL

Reputation: 12907

Simple. Header guards in every single header.

The way you've done it is unsafe : What if one day someone (not necessarily you, though even this is unsure) includes directly one of the headers you listed (though these are mostly external libs it seems, but it could evolve to include one of yours...), and this header doesn't have include guards ? Might as well exclude this possible issue ASAP !

To structure your headers, you should prefer to include what is strictly needed rather than everything in one global header.

Edit answer : No you won't include it twice. After the first include, every extra occurrence of the header guarded file will simply be ignored.

Upvotes: 3

zakinster
zakinster

Reputation: 10688

It's not a good idea to put all your includes in one files, except if you always include all those file.

You should only include the strict minimum of required headers in your own headers and include the rest directly in your .cpp source files.

Each of your headers should have a unique header guard without conflict with any other library, so take a very good care of the naming scheme.

You may also consider using the non-standard #pragma once directive, if you're not writing portable code.

You could take a look at this paper about the best practice for designing header files

To answer your edit :

No you don't include <glm/glm.hpp> twice, because it has itself a header guard. But you should include it only if you actually need glm.hpp inside your header, otherwise you should include it later. Note that you can often avoid the include by forward-declaring what you need, that could speed-up the compilation and avoid circular dependency, but that's another issue.

Upvotes: 6

Related Questions