# Why am I getting these errors when including stb_image.h?

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
#ifndef STBI_INCLUDE_STB_IMAGE_H
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#endif


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
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#endif

• stb_image.h already has it's own include guard. You don't need to write it yourself. – 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.

• IOW, STB_IMAGE_IMPLEMENTATION is not the include guard for stb_image.h. – MSalters May 2 '18 at 14:43