SFML community forums
Help => General => Topic started by: Niidhogg on September 26, 2020, 12:34:23 am
-
Hej !
I'm just working on a new project in SFML on Archlinux and everything went well, until I tried to load a Font. I don't really understanf why i get that error.
HW_Ressource.h
#ifndef HW_RESSOURCES_H
#define HW_RESSOURCES_H
#include <SFML/Graphics.hpp>
class HW_Ressources
{
private:
HW_Ressources();
static sf::Font font_cantarell;
public:
static void load();
static const sf::Font get_font_cantarell();
};
#endif
HW_Ressources.cpp
#include "HW_Ressources.h"
void HW_Ressources::load()
{
sf::Font font;
if(!font.loadFromFile("Cantarell-Regular.otf"))
exit(EXIT_FAILURE);
else HW_Ressources::font_cantarell = sf::Font(font);
}
const sf::Font HW_Ressources::get_font_cantarell()
{
return HW_Ressources::font_cantarell;
}
COMPILING:
g++ -std=gnu++11 -Wall -Werror *.cpp -o exe -lsfml-graphics -lsfml-window -lsfml-system ; ./exe
/usr/bin/ld: /tmp/cciPHgtd.o: warning: relocation against `_ZN13HW_Ressources14font_cantarellE' in read-only section `.text'
/usr/bin/ld: /tmp/cciPHgtd.o: in function `HW_Ressources::load()':
HW_Ressources.cpp:(.text+0xc6): undefined reference to `HW_Ressources::font_cantarell'
/usr/bin/ld: /tmp/cciPHgtd.o: in function `HW_Ressources::get_font_cantarell()':
HW_Ressources.cpp:(.text+0x177): undefined reference to `HW_Ressources::font_cantarell'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
zsh: no such file or directory: ./exe
-
I don't really understand the error either, but I don't recommend what you're doing.
SFML resources like a font should never be static/globally initialized, this will crash on some systems, due to undefined global initialization order.
Additionally you're creating multiple copies of a font object, that's quite expensive.
My recommendation is to use something lile a resource holder: https://github.com/SFML/SFML-Game-Development-Book/tree/master/02_Resources/Include/Book
-
That helps, thx !