Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stb_image.h in Visual Studio - unresolved external symbol

I've downloaded stb_image.h (https://github.com/nothings/stb) and included in the .cpp file I want to use it.

Then I use the function to load an image

image_data = stbi_load(fileNames[i], &image_width, &image_height, &image_pixel_components, 0);

When I try to run the code in Visual Studio I get an error:

Error   3   error LNK2019: unresolved external symbol _stbi_load referenced in function "public: bool __thiscall CubemapTexture::Load(void)" (?Load@CubemapTexture@@QAE_NXZ)    ...\CubemapTexture.obj

Error   40  error LNK2001: unresolved external symbol _stbi_load    ...\Texture.obj
like image 469
andrepcg Avatar asked Dec 01 '22 01:12

andrepcg


1 Answers

The answer is right at the top of stb_image.h:

Do this:

  #define STB_IMAGE_IMPLEMENTATION    

before you include this file in one C or C++ file to create the implementation.
i.e. it should look like this:

#include ....
#define STB_IMAGE_IMPLEMENTATION    
#include "stb_image.h"

Without this define, you will get errors about unresolved external symbols.

like image 115
Jan Šimek Avatar answered Dec 05 '22 10:12

Jan Šimek