Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How can I use stbi writing functions?  (Read 26 times)

0 Members and 1 Guest are viewing this topic.

GetterSetter

  • Newbie
  • *
  • Posts: 14
    • View Profile
How can I use stbi writing functions?
« on: April 28, 2024, 08:20:55 pm »
Hello, if I try to include stbi headers, I receive an error from the linker about multiple definition of stbi_write_.... If I try to use
extern int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
//and so on
 
I receive the following error: undefined reference to stbi_... and it doesn't matter which link order I use. So, how can I use these functions? I need to do it because I have the set of pixels itself, so I think that creating a sf::Texture, updating it with this set, then copying it to an sf::Image and finally calling saveToFile() is a bit inefficient and stupid.
RMK: I'm using static sfml libraries
« Last Edit: April 28, 2024, 09:03:10 pm by GetterSetter »

kojack

  • Sr. Member
  • ****
  • Posts: 315
  • C++/C# game dev teacher.
    • View Profile
Re: How can I use stbi writing functions?
« Reply #1 on: April 28, 2024, 11:04:39 pm »
In one (and only one) of your cpp files that includes the stbi headers, you need to do some defines before the includes:
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
 
The first one is needed for the reading functions. The second one for the writing functions.

GetterSetter

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: How can I use stbi writing functions?
« Reply #2 on: April 28, 2024, 11:31:23 pm »
Yeah, I tried it before starting the topic, and I got the error about multiple definitions of these functions because, I assume, they are already implemented in libsfml-graphics.a.
Oh, I think, I've got your idea. Do you mean that in my cpp file I should only include headers without using defines?
« Last Edit: April 28, 2024, 11:34:29 pm by GetterSetter »

 

anything