11

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

Community
  • 1
  • 1
cppbeginner
  • 111
  • 1
  • 1
  • 3
  • 17
    Did you note the doc near the top of `stb_image.h`. As I understand this: You should include `stb_image.h` in `a.cpp` with pre-`#define STB_IMAGE_IMPLEMENTATION` and in `main.cpp` (and anywhere else) without. (This shall probably grant that definitions are done only once otherwise you could get the link issues you actually have now.) – Scheff's Cat Apr 11 '17 at 14:54
  • The comment above worked for me, this should be accepted as the answer. – Plasty Grove Oct 13 '18 at 17:19
  • I should read the documentation really. This solution perfectly works! – bim May 05 '22 at 12:58

1 Answers1

10
  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;
}
Valery S.
  • 596
  • 1
  • 4
  • 10