2

When I try to use stbi to load an image all I get is this error:

1>Renderer.obj : error LNK2005: "void __cdecl stbi__unpremultiply_on_load_thread(int)" (?stbi__unpremultiply_on_load_thread@@YAXH@Z) already defined in LoadFile.obj

1>Main.obj : error LNK2005: "void __cdecl stbi__unpremultiply_on_load_thread(int)" (?stbi__unpremultiply_on_load_thread@@YAXH@Z) already defined in LoadFile.obj

1>Shape.obj : error LNK2005: "void __cdecl stbi__unpremultiply_on_load_thread(int)" (?stbi__unpremultiply_on_load_thread@@YAXH@Z) already defined in LoadFile.obj

1>Texture.obj : error LNK2005: "void __cdecl stbi__unpremultiply_on_load_thread(int)" (?stbi__unpremultiply_on_load_thread@@YAXH@Z) already defined in LoadFile.obj

and I get about 180 of these errors for different functions.

I include the stbi in one header file and I use this define

#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
  • 2
    Do you define `STB_IMAGE_IMPLEMENTATION` in every file that you `#include `, or just in one of your files? – jjramsey Jan 05 '22 at 18:06
  • "I include the header file in one file and I use this define": Is that one file a single `.cpp` file or is it itself a header file included in multiple other files? – user17732522 Jan 05 '22 at 18:08
  • @jjramsey I only `#include ` in one file and only use the define in that same file. – Immanuel Charles Jan 05 '22 at 18:09
  • @user17732522 It is a header file included in multiple files – Immanuel Charles Jan 05 '22 at 18:10
  • Does this answer your question? [Double inclusion and headers only library stbi\_image](https://stackoverflow.com/questions/43348798/double-inclusion-and-headers-only-library-stbi-image) and https://gamedev.stackexchange.com/questions/158106/why-am-i-getting-these-errors-when-including-stb-image-h – user17732522 Jan 05 '22 at 18:13
  • 3
    You must include `STB_IMAGE_IMPLEMENTATION` in only one translation unit. Including it in a header that is included in multiple translation units doesn't satisfy this requirement. – user17732522 Jan 05 '22 at 18:13

2 Answers2

1

If you have multiple .cpp files that use the functions of stb_image.h, including main.cpp, put #define STB_IMAGE_IMPLEMENTATION in main.cpp before the header file that includes stb_image.h.

The structure will look like this:

header1.h

#pragma once
#include "some_header_file.h"
#include "stb_image.h"

void someFunction ();

source1.cpp

#include "header1.h"

void someFunction ()
{
    ...
    // code line which uses functions in stb_image.h, e.g.
    unsigned char* IMGpixels = stbi_load(imgFile, &IMGwidth, &IMGheight, &IMGchannels, 4);
}

main.cpp

#define STB_IMAGE_IMPLEMENTATION // should define this before include stb_image.h, 
#include "header1.h"

int main(int argc, char** argv)
{
    ...
    // code line which uses functions in stb_image.h, e.g.
    unsigned char* data = stbi_load(texFilename, &texWidth, &texHeight, &colorChannels, 0);
}
0
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

Put this in stb_image.cpp

pedro_bb7
  • 1,601
  • 3
  • 12
  • 28