【问题标题】:I can't compile example code, SFML我无法编译示例代码,SFML
【发布时间】:2015-08-13 14:50:10
【问题描述】:

我看到了类似的问题,但它并没有解决我的问题。 我无法编译此示例代码 http://www.sfml-dev.org/tutorials/2.3/start-linux.php 。我正在按照此说明进行操作,但仍有错误:

在最后一步之前,我写道:g++ sfml.o -o sfml-app -L /home/Documents/SFML/SFML-2.1/lib -lsfml-graphics -lsfml-window -lsfml-system

我得到了这个:

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 返回1 退出状态

有人可以帮我吗?

【问题讨论】:

  • 你能粘贴你的代码吗?另外,你确定所有的库都在/home/Documents/SFML/SFML-2.1/lib吗?

标签: c++ linker sfml


【解决方案1】:

您似乎没有将 sfml 库链接到您的程序。在该页面的下方(代码 sn-p 之后),该页面进一步描述:

然后,您必须将编译后的文件链接到 SFML 库才能获得最终的可执行文件。 SFML 由 5 个模块(系统、窗口、图形、网络和音频)组成,每个模块都有一个库。 要链接 SFML 库,您必须在命令行中添加“-lsfml-xxx”,例如图形模块的“-lsfml-graphics”(库文件名的“lib”前缀和“.so”扩展名必须省略)。

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

如果您将 SFML 安装到非标准路径,则需要告诉链接器在哪里可以找到 SFML 库(.so 文件):

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

虽然你已经在命令行中包含了链接,但我最好的猜测是链接器仍然找不到库,这就是 sf::RenderWindow 未定义的原因(它已声明,因此编译器知道它是什么,因此编译成功,但无法正确链接,因为链接器找不到它的引用)

【讨论】:

    【解决方案2】:

    我检查了带有库和库的路径和文件夹: http://i.imgur.com/bTk7QCZ.png

    但我仍然无法正确链接它。我已在标准路径 (usr/lib) 中安装了 SFML http://i.imgur.com/Mzp9tMY.png 编译后显示同样的错误。

    错误: sfml.o:在函数main': sfml.cpp:(.text+0x12d): undefined reference tosf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&)' collect2: ld 返回 1 个退出状态

    我的代码和示例代码一样:

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

    【讨论】:

      猜你喜欢
      • 2015-07-03
      • 2018-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多