Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double inclusion and headers only library stbi_image

I have a main.cpp including a.h (that has its own a.cpp) a.h includes the header only library "stbi_image.h" as such:

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

(https://github.com/nothings/stb)

*.cpp includes its own *.h with the use of #pragma once

But I still get:

LNK1169 one or more multiply defined symbols found LNK2005 stb-failure reason already defined in a.obj file = main.obj ... and a bunch of others

It seems right to me, but as I understand in this question: Multiple definition and header-only libraries

Maybe I should add inline/static to the stb_image.h function I need ? Am I doing something wrong?

Thanks in advance

like image 494
cppbeginner Avatar asked Apr 11 '17 14:04

cppbeginner


People also ask

What is Stb_image?

// stb_image supports loading HDR images in general, and currently the Radiance. // .HDR file format specifically. You can still load any file through the existing. // interface; if you attempt to load an HDR file, it will be automatically remapped.

What is STB library?

stb. h is a (theoretically) portable C utility library, which provides numerous features.


1 Answers

  1. Maybe I should add inline/static to the stb_image.h function I need ?

No, you already have a way to declare 'stb_image functions' as static or as extern:

#define STB_IMAGE_STATIC
  1. Am I doing something wrong ? Yes, you compile 'stb_image' twice, each time you include 'stb_image.h' So, the whole design could be:

Image.h:

#ifndef _IMAGE_H_
#define _IMAGE_H_

Class Image.h {
public:
    Image() : _imgData(NULL) {}
    virtual ~Image();
    ...
    void loadf(...);
    ...

    unsigned char* getData() const { return _imgData; }
protected:
    unsigned char* _imgData;
};
#endif

Image.cpp:

#include "Image.h"

#define STB_IMAGE_IMPLEMENTATION   // use of stb functions once and for all
#include "stb_image.h"

Image::~Image()
{ 
    if ( _imgData ) 
        stbi_image_free(_imgData); 
}

void Image::load(...) {
    _imgData = stbi_load(...);
}

main.cpp

#include "Image.h" // as you see, main.cpp do not know anything about stb stuff

int main() {
    Image* img = new Image();  // this is my 'wrapper' to stb functions
    img->load(...);

    myTexture(img->getData(), ...);

    return 0;
}
like image 182
Valery S. Avatar answered Oct 02 '22 15:10

Valery S.