链接器错误与SFML

时间:2015-06-03 21:04:30

标签: c++ linker-errors sfml

在运行Ubuntu,SFML 2.2和g ++ 4.8.2时,在示例SFML代码上运行g++ -lsfml-window -lsfml-graphics -lsfml-system main.cpp时出现一系列错误。我尝试过从包管理器(libsfml-dev)重新安装SFML,但无效。

示例SFML代码:

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

错误讯息:

/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

4 个答案:

答案 0 :(得分:5)

有两种方法可以解决此问题。第一种是交换一些选项,所以命令如下:g++ main.cpp -lsfml-window -lsfml-graphics -lsfml-system。第二个选择是尝试将g ++更新到版本4.9.2,这可以在ubuntu上实现by doing this

答案 1 :(得分:0)

clang ++ - 3.6(包 clang-3.6 基于LLVM)适用于我,是包装回购的一部分。 g ++ - 4.8.2也不能用于重新排序库。

答案 2 :(得分:0)

您缺少GCC的编译命令,该命令为g++ -c。 这将为您提供一个默认的目标文件,您可以从终端运行该文件。 如果有帮助,那么我一直可以使用的标准脚本将是程序=>文件=>包含/库=>对象=>链接器文件

在这种情况下,一个例子是
控制台:g++ -c main.cpp -o sfmlApp -lsfml-window -lsfml-graphics -lsfml-system
控制台:./sfmlApp

-c是编译对象,-o是默认对象文件的名称。

如果在大多数Linux发行版上使用libsfml-dev,则可能必须在包含文件和库文件后附加编译和对象实例。本教程将其显示为添加:
控制台:g++ -c main.cpp -o sfmlApp -L /sfml-install-path/lib -lsfml-graphics -lsfml-window -lsfml-system

其中-L /sfml-install-path/lib是Linux版本从网站上的实际下载位置。如果我记得,这应该适用于Ubuntu,但是您可能还需要增加一步,如网站教程中所示,以开始使用该系统的开销。

export LD_LIBRARY_PATH=/sfml-install-path/lib && ./sfml-app
g++ -c main.cpp -o sfmlApp -lsfml-window -lsfml-graphics -lsfml-system

那至少应该让您入门。

答案 3 :(得分:0)

解决方案很简单。您没有以正确的顺序传递参数。

正确的订单

('127.0.0.1', 50086)
('127.0.0.1', 18812)

您需要始终以正确的顺序传递参数。

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