Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linker error with SFML

I get a series of errors when running g++ -lsfml-window -lsfml-graphics -lsfml-system main.cpp on the example SFML code, when running Ubuntu, SFML 2.2 and g++ 4.8.2. I've tried reinstalling SFML from the package manager (libsfml-dev) and nothing works.

Example SFML Code:

#include <SFML/Graphics.hpp>
#include <string>

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;
}

Error Message:

/tmp/ccVG6GjG.o: In function `main':
main.cpp:(.text+0xf7): undefined reference to `sf::String::String(char const*, std::locale const&)'
main.cpp:(.text+0x115): undefined reference to `sf::VideoMode::VideoMode(unsigned int, unsigned int, unsigned int)'
main.cpp:(.text+0x148): undefined reference to `sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings cons'
main.cpp:(.text+0x182): undefined reference to `sf::CircleShape::CircleShape(float, unsigned int)'
main.cpp:(.text+0x18e): undefined reference to `sf::Color::Green'
main.cpp:(.text+0x196): undefined reference to `sf::Shape::setFillColor(sf::Color const&)'
main.cpp:(.text+0x1b6): undefined reference to `sf::Window::close()'
main.cpp:(.text+0x1cf): undefined reference to `sf::Window::pollEvent(sf::Event&)'
main.cpp:(.text+0x1f7): undefined reference to `sf::Color::Color(unsigned char, unsigned char, unsigned char, unsigned char)'
main.cpp:(.text+0x214): undefined reference to `sf::RenderTarget::clear(sf::Color const&)'
main.cpp:(.text+0x22b): undefined reference to `sf::RenderStates::Default'
main.cpp:(.text+0x236): undefined reference to `sf::RenderTarget::draw(sf::Drawable const&, sf::RenderStates const&)'
main.cpp:(.text+0x245): undefined reference to `sf::Window::display()'
main.cpp:(.text+0x254): undefined reference to `sf::Window::isOpen() const'
main.cpp:(.text+0x27f): undefined reference to `sf::RenderWindow::~RenderWindow()'
main.cpp:(.text+0x2a9): undefined reference to `sf::RenderWindow::~RenderWindow()'
main.cpp:(.text+0x2ee): undefined reference to `sf::RenderWindow::~RenderWindow()'
/tmp/ccVG6GjG.o: In function `sf::CircleShape::~CircleShape()':
main.cpp:(.text._ZN2sf11CircleShapeD2Ev[_ZN2sf11CircleShapeD5Ev]+0x13): undefined reference to `vtable for sf::CircleShape'
main.cpp:(.text._ZN2sf11CircleShapeD2Ev[_ZN2sf11CircleShapeD5Ev]+0x1f): undefined reference to `vtable for sf::CircleShape'
main.cpp:(.text._ZN2sf11CircleShapeD2Ev[_ZN2sf11CircleShapeD5Ev]+0x2b): undefined reference to `sf::Shape::~Shape()'
collect2: error: ld returned 1 exit status
like image 623
bboy3577 Avatar asked Feb 10 '23 23:02

bboy3577


1 Answers

There are two ways to solve this issue. The first is to swap some of the options so the command goes like this: g++ main.cpp -lsfml-window -lsfml-graphics -lsfml-system. The second option is to try updating g++ to version 4.9.2, which can be achieved on ubuntu by doing this

like image 138
bboy3577 Avatar answered Feb 13 '23 04:02

bboy3577