SFML 2.1 RenderWindow linking error

2019-07-25 00:15发布

问题:

My code:

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");

    while(window.isOpen())
    {
        sf::Event Event; 
        while(window.pollEvent(Event))
        {
            if(Event.type == sf::Event::Closed || Event.key.code == sf::Keyboard::Escape)
                window.close();
        }

        window.display();
    }
    return 0; 
}

My compiler call:

g++ main.cpp -framework SFML -lsfml-graphics -lsfml-window -lsfml-system

The error message:

Undefined symbols for architecture x86_64:
      "sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&)", referenced from:
      _main in cc8BMfpR.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

Is RenderWindow in a library I forgot to link? I assumed it was in the 'window' one.. I'm running SFML 2.1 and tried various linking combinations, all of which give me linking errors. This linking chain is the one that gives me the least error, namely the RenderWindow error. Someone help me figure this out? I'm at a bit of a loss here. I'm running on mac os 10.8.

回答1:

Is RenderWindow in a library I forgot to link? I assumed it was in the 'window' one..

It's in the graphics package. Hence in sfml-graphics. But you already linked that one. However, you're not suppose to link against SFML.framework (it contains only header files).

As stated here, you can use either frameworks or dylibs. Your program can be compiled with either:

g++ main.cpp -lsfml-graphics -lsfml-window -lsfml-system

or

g++ main.cpp -framework sfml-graphics -framework sfml-window -framework sfml-system

Now, regarding the Undefined symbols for architecture x86_64 error, I can only guess you didn't download the compatible version from the download page. If you want to use g++, download the "GCC" version.

Or, switch to clang. (E.g., you can see here that clang can be faster than GCC.)