I'm trying to put an include guard around including stb_image.h (from the well-known public domain image library).

I have noticed that, unusually, the include guard works inside the .cpp file but not inside the header file.

Let me explain: Inside texture.h:

//This is something that stb image defines in its own include guard, so I should be able to test for it
#include "stb_image.h"

This gives me errors (multiple definitions of everything inside stb_image.h), but the same code in Texture.cpp does not give me an error.

Also, I'd like to note that this does not work either:

#ifndef WHOMST
#define WHOMST
#include "stb_image.h"
  • \$\begingroup\$ stb_image.h already has it's own include guard. You don't need to write it yourself. \$\endgroup\$ – HolyBlackCat May 2 '18 at 14:02

From the stb_image.h file itself:

Do this: #define STB_IMAGE_IMPLEMENTATION before you include this file in one C or C++ file to create the implementation.

You must not #define STB_IMAGE_IMPLEMENTATION in header (.h) files.

Only in one C/C++ file to create the implementation (the stuff that has to be unique and done only once.)

The header files can include stb_image.h but must not have this preprocessor macro defined.

Screenshot with added highlight:

enter image description here

| improve this answer | |
  • \$\begingroup\$ IOW, STB_IMAGE_IMPLEMENTATION is not the include guard for stb_image.h. \$\endgroup\$ – MSalters May 2 '18 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.