简体   繁体   中英

I can't compile example code, SFML

I saw similiar problem, but it didn't solved me my problem. I can't compile this example code http://www.sfml-dev.org/tutorials/2.3/start-linux.php . I am following this instructions but I still have error:

In one before last step I wrote : g++ sfml.o -o sfml-app -L /home/Documents/SFML/SFML-2.1/lib -lsfml-graphics -lsfml-window -lsfml-system

and I got this:

sfml.o: In function main':

sfml.cpp:(.text+0x12d): undefined reference to sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&)' collect2: ld returned 1 exit status

Can somebody help me with that?

It seems like you have't linked the sfml libraries to your program. Further down that page (just after the code snippet), the page further describes:

You must then link the compiled file to the SFML libraries in order to get the final executable. SFML is made of 5 modules (system, window, graphics, network and audio), and there's one library for each of them. To link an SFML library, you must add "-lsfml-xxx" to your command line, for example "-lsfml-graphics" for the graphics module (the "lib" prefix and the ".so" extension of the library file name must be omitted).

g++ main.o -o sfml-app -lsfml-graphics -lsfml-window -lsfml-system

If you installed SFML to a non-standard path, you'll need to tell the linker where to find the SFML libraries (.so files):

g++ main.o -o sfml-app -L<sfml-install-path>/lib -lsfml-graphics -lsfml-window -lsfml-system

Although you have included the linking in your command line, my best guess is that the linker cannot find the libraries still, which is why sf::RenderWindow is undefined (it's declared, so the compiler knows what it is and thus compiles successfully, but cannot link properly because the linker can't find a reference for it)

I checked the path and folder with libraries and libs are there: http://i.imgur.com/bTk7QCZ.png

But I still can't linkt it correctly.. I have installed SFML in my standard path (usr/lib) http://i.imgur.com/Mzp9tMY.png and after compile it shows the same error.

Error: sfml.o: In function main': sfml.cpp:(.text+0x12d): undefined reference to sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&)' collect2: ld returned 1 exit status

My code it's the same as example code:

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(shape);
        window.display();
    }

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM