I get many errors when including stb_image.h

Hello, I am working with stb library. This is my texture.cpp file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
class Texture
{
public:
    const char* path;
    float x;
    float y;
    bool visible;
    int img_width;
    int img_height;
    int rgb_channels;
    Texture(const char* path_, float x_, float y_)
    {
        path = path_;
        visible = true;
        x = x_;
        y = y_;
    }
    Texture()
    {

    }
    void show_image()
    {
        int img_width_, img_height_, img_channels_;
        unsigned char* data = stbi_load(path, &img_width_, &img_height_, &img_channels_, 0);
        img_width = img_width_;
        img_height = img_height_;
        rgb_channels = img_channels_;
        int index = 0;
        if (data != nullptr && img_height > 0 && img_width > 0)
        {
            for (int i = 0; i < img_height; i++)
            {
                for (int j = 0; j < img_width; j++)
                {
                    index++;
                    //draw_pixel(x + j * 0.1f, y + i * 0.1f, rgb(data[index], data[index + 1], data[index + 2]));
                    //std::cout << index << " pixel: RGB " << static_cast<int>(data[index]) << " " << static_cast<int>(data[index + 1]) << " " << static_cast<int>(data[index + 2]) << std::endl;
                }
            }
        }
        stbi_image_free(data);
    }
};

I don't get any errors when compiling texture.cpp. But when i include the scipt into another file it gives me many strange errors:
1
2
3
4
5
#include "texture.cpp"
static void simulategame()
{

}
Last edited on
Well, first, you shouldn't ever be #including a .cpp file. So honestly this thread can end there.

But to be more correct:
The STB_IMAGE_IMPLEMENTATION macro needs to be in exactly one compilation unit (one .cpp file).
https://stackoverflow.com/questions/43348798/double-inclusion-and-headers-only-library-stbi-image

Move the definition of your show_image function to a .cpp file and #define STB_IMAGE_IMPLEMENTATION inside that .cpp file.

Don't put STB_IMAGE_IMPLEMENTATION in a header file.
Last edited on
Oh, it worked. Thanks!
Topic archived. No new replies allowed.