c++undefined-referencestb-image

Undefined references to stb_image functions?


I want to load images using stb_image. I downloaded stb_image.h from https://github.com/nothings/stb.

When I run the code:

string file="image.png";
int width,height,components;

unsigned char *imageData = stbi_load(file.c_str(),
        &width, &height, &components, STBI_rgb_alpha);

...I get the following errors:

Main.cpp:(.text+0xa14): undefined reference to `stbi_load'
Main.cpp:(.text+0xb74): undefined reference to `stbi_image_free'

Solution

  • You probably should add: #define STB_IMAGE_IMPLEMENTATION to your code before the include. This is suggested in one of the first lines of the header file.