SFML community forums

Help => General => Topic started by: ThomasAn. on April 25, 2013, 06:39:35 am

Title: sfml 2.0 undefined reference to getDefaultFont
Post by: ThomasAn. on April 25, 2013, 06:39:35 am
I am writing a simple game and so far all works well, with graphics and keyboard working well, but ...
... today for the first time I am trying to add some text elements and get this error.

Just doing the text sample from the doc page (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Text.php):
(And I have the Arial.ttf in the local folder)

Quote
// Declare and load a font
 sf::Font font;
 font.loadFromFile("arial.ttf");
 
 // Create a text
 sf::Text text("hello");
 text.setFont(font);
 text.setCharacterSize(30);
 text.setStyle(sf::Text::Bold);
 text.setColor(sf::Color::Red);

 // Draw it
 window.draw(text);
Title: AW: sfml 2.0 undefined reference to getDefaultFont
Post by: eXpl0it3r on April 25, 2013, 07:51:03 am
From where did you get SFML from?
In the latest version there's no default font. To me it seems like you're mixong old header files with the new SFML 2 lib files. ;)
Title: Re: AW: sfml 2.0 undefined reference to getDefaultFont
Post by: OniLinkPlus on April 26, 2013, 12:09:24 am
From where did you get SFML from?
In the latest version there's no default font. To me it seems like you're mixong old header files with the new SFML 2 lib files. ;)
He's not using a default font. He's loading it from arial.ttf.
Title: Re: AW: sfml 2.0 undefined reference to getDefaultFont
Post by: eXpl0it3r on April 26, 2013, 12:29:08 am
He's not using a default font. He's loading it from arial.ttf.
I know, I can ready C++ code... :P

The problem is, that he apparently gets undefined reference to getDefaultFont "without" using the default font.
The linker error occurs, when there's a function defined in the header, but can't be found in the library. And thus my conclusion, he's using old SFML 2.0 headers with the new SFML 2.0 library binaries.
With the new headers the code would error much sooner, since there's now only the default constructor or the constructor which has to take a sf::Font. ;)
Title: Re: sfml 2.0 undefined reference to getDefaultFont
Post by: ThomasAn. on April 26, 2013, 01:06:51 am
aaahm, thanks for the ideas ... but I am confused still :-)

I downloaded SFML 2.0 from this website directly, back in December (5months ago ? I believe) and I remember taking the extra steps to make a clean installation, as a matter of fact I spent a bit of time understanding how to built/compile SFML for my system ... and I remember it went smoothly with no errors at the time. (Of course I have forgotten most of those methods now, but I do remember having to download a few open source tools).

In any case, I have been writing a small game the past two weeks with no errors, that is until I tried to add some text elements yesterday. (It gives the same error regardless of whether I explicitly specify a font, or leave font info completely out)

So you guys think everything is properly linked except the text headers ? (by saying "everything" ... well I haven't checked audio, but so far keyboard, mouse, and graphics are fine)
Title: Re: sfml 2.0 undefined reference to getDefaultFont
Post by: eXpl0it3r on April 26, 2013, 01:26:40 am
So you guys think everything is properly linked except the text headers ? (by saying "everything" ... well I haven't checked audio, but so far keyboard, mouse, and graphics are fine)
You should make sure, that the header files actually match the library you're using.

I downloaded SFML 2.0 from this website directly, back in December (5months ago ? I believe) and I remember taking the extra steps to make a clean installation, as a matter of fact I spent a bit of time understanding how to built/compile SFML for my system ... and I remember it went smoothly with no errors at the time.
So you've downloaded the RC from the website or the snapshot?
Because if you've downloaded the RC and then still compiled SFML, but now are using the headers from the RC and the self build libs from GitHub, then you've the chaos... ;)

In any case, I have been writing a small game the past two weeks with no errors, that is until I tried to add some text elements yesterday. (It gives the same error regardless of whether I explicitly specify a font, or leave font info completely out)
Try this for a change:

#include <SFML/Graphics.hpp>

int main()
{
    sf::Window window(sf::VideoMode(800, 600), "Test Text");
    sf::Font font;
    font.loadFromFile("arial.ttf");

    sf::Text text("Hello! What?", font, 30);

    window(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();
        window.draw(text);
        window.display();
    }
}
Title: Re: sfml 2.0 undefined reference to getDefaultFont
Post by: ThomasAn. on April 26, 2013, 07:04:22 am
Yup !
Your example works.

I see you used the explicit constructor:  sf::Text text("hello", font, 30);
That was the key (although I thought I tried that explicit call earlier, maybe something else got in the way)

In any case if I use only  sf::Text text("hello");
it complains, but I don't care :-)  ... I got text now !

Thank you for your help on this.
Title: AW: sfml 2.0 undefined reference to getDefaultFont
Post by: eXpl0it3r on April 26, 2013, 08:43:24 am
You should really make your headers and binaries match each other instead...
Title: Re: sfml 2.0 undefined reference to getDefaultFont
Post by: santiaboy on April 26, 2013, 08:57:23 pm
That's because when you call sf::Text text("hello"); it interprents it as  sf::Text text("hello", Font::getDefaultFont(), 30); whereas if you call it with the defualt constructor, it doesn't.

Basically you can do this:
sf::Text myText;
myText.setString("Blah");
myText.setFont(myFont);
myText.setCharacterSize(30);

or this:
sf::Text myText("Blah", myFont, 30);

PS: you can ommit the 30 as, BUT you can't ommit the font, because it calls defaultFont, which doesn't exist anymore

More info: http://www.sfml-dev.org/documentation/2.0/classsf_1_1Text.php