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

Author Topic: Relocation against in read only section ERROR when trying to load font  (Read 11440 times)

0 Members and 1 Guest are viewing this topic.

Niidhogg

  • Newbie
  • *
  • Posts: 4
    • View Profile
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
Quote
#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

Quote
#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:

Quote
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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: Relocation against in read only section ERROR when trying to load font
« Reply #1 on: September 26, 2020, 09:37:08 am »
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
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Niidhogg

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Relocation against in read only section ERROR when trying to load font
« Reply #2 on: September 27, 2020, 12:38:58 am »
That helps, thx !