Reputation: 121
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
Upvotes: 12
Views: 13417
Reputation: 34
In addition to the solution mentioned by @Scheff's Cat below the question, I would like to point out the reason to do this.
STB_IMAGE_IMPLEMENTATION
is not the protection for the header file. When STB_IMAGE_IMPLEMENTATION
is enabled, you are actually inserting the corresponding implementation code in that file, so you cannot put it in a header .
Upvotes: 0
Reputation: 636
No, you already have a way to declare 'stb_image functions' as static or as extern:
#define STB_IMAGE_STATIC
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;
}
Upvotes: 11