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>
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);
}