SFML loadFromFile undefined reference

2019-02-28 17:06发布

问题:

I'm learning to use SFML with the idea of making a small game, so far I've created a window and messed about with it's settings, and set up a draw loop and event handler.

The trouble started once I tried to add my first texture using the following code:

#include "SpaceNomad.ih"

void MainMenu(GameEngine &Engine)
{
    sf::Texture texture;

    if(!texture.loadFromFile("MenuBackGround.png"))
    {
        cout << "couldn't load background texture\n";
    }

    sf::Sprite *sprite = new sf::Sprite;
    sprite->setTexture(texture);

    Engine.AddEntity(sprite, 5);
}

which is outside of a different image literally the example code given on the SFML tutorial page.

When I try to compile this I get the following error:

||=== Build: Debug in space nomad (compiler: GNU GCC Compiler) ===|
obj\Debug\projects\st\take2\MainMenu.o||In function     `Z8MainMenuR10GameEngine':|
D:\projects\st\take2\MainMenu.cpp|7|undefined reference to `_imp___ZN2sf7Texture12loadFromFileERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_4RectIiEE'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

I have checked that all the sfml libraries are included in the build options(I'm using code::Blocks with GCC MinGW).

https://drive.google.com/file/d/0B4qzXcgqbLZtcHJWUy1vSUhpdVk/view?usp=sharing

Other topics I've seen deal with undefined references to functions people have made themselves, but here I'm using a library.

edit: I've just tried with the basic shape from the same libary:

    sf::CircleShape *shape = new sf::CircleShape(50);

    // set the shape color to green
    shape->setFillColor(sf::Color(100, 250, 50));

    Engine.AddEntity(shape, 5);

Which compiles and displays without a problem.

回答1:

You're using SFML libraries which were compiled with a different runtime or a different runtime ABI than you link your application against.

If your SFML libraries weren't compiled with the same compiler that you have, then you need to rebuild.

If you have set any special flags on your application (e.g. different ABI, C++14, etc.) you'll have to rebuild SFML with the same flags as well or remove them from your project.

Also it only happens sometimes because it doesn't affect the whole runtime libraries but parts like std::string which get used when calling the loadFromFile function.



标签: c++ sfml