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

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

0 Members and 1 Guest are viewing this topic.

GetterSetter

  • Newbie
  • *
  • Posts: 18
    • 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: 343
  • 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: 18
    • 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 »

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: How can I use stbi writing functions?
« Reply #3 on: April 29, 2024, 09:20:04 am »
Basically stbi needs a single instance of some things that can't safely be done in a header. Those defines let it create the objects that it needs.
If you used the stbi headers in for example 5 cpp files, you need to do the defines in just one cpp, the other cpps would do the includes without defines. If you do the defines in more than one cpp, you'll get the multiple definition error.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: How can I use stbi writing functions?
« Reply #4 on: April 29, 2024, 09:50:25 am »
As a side note: While SFML does use stbi, it shouldn't leak outside the SFML bounds
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

GetterSetter

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How can I use stbi writing functions?
« Reply #5 on: April 29, 2024, 10:51:20 am »
Thank you, kojack!

 

anything