Search code examples
c++sfml

STB Image Implementation with tinygltf and sfml


I am including both tinygltf and sfml in my project. When I include tinygltf with these options:

#define TINYGLTF_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <tiny_gltf.h>

I get multiple definition errors:

/usr/bin/ld.gold: error: sfml/lib/libsfml-graphics-s.a(ImageLoader.cpp.o): multiple definition of 'stbi_write_jpg'
/usr/bin/ld.gold: _objs/client_lib/loader.pic.o: previous definition here

That is just a small snippet of the errors, but for most stb functions, there is a similar error. So I tried to fix this by including tinygltf like this:

#define TINYGLTF_IMPLEMENTATION
#include <tiny_gltf.h>

However, now there is just one singular error:

_objs/client_lib/loader.pic.o:loader.cpp:function tinygltf::LoadImageData(tinygltf::Image*, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, int, int, unsigned char const*, int, void*): error: undefined reference to 'stbi_is_16_bit_from_memory'

It seems like the two libraries are depending on slightly different versions of stb. Is there any way to fix this?

I'm using tinygltf version 2.2.0, and sfml version 2.5.1.


Solution

  • Since you're linking SFML statically, you're integrating all the symbols of SFML in your application including the STB symbols.

    I see two different solutions here, maybe there are more:

    • Update the STB header in the SFML extlibs/ directory and rebuild SFML
    • Link SFML dynamically, so your application doesn't see the SFML STB symbols