10

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'
0

1 Answer 1

39

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.

Sign up to request clarification or add additional context in comments.

5 Comments

It worked! Now I can compile it but when I try loading an image the program crashes, any ideas?
What is the error with which the program crashes?
I get a Segmentation fault. but it was caused by the next line: glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTextureParameteri is a GL 4.5 function which must be loaded via the GL extension mechanism, and you are even using it wrong. You are probably looking for glTexParameteri
Thank you so much it works perfectly now!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.