【问题标题】:Linker Error when using string literal for sf::String对 sf::String 使用字符串文字时出现链接器错误
【发布时间】:2013-09-18 17:26:16
【问题描述】:

在使用 XCode 处理 C++11 和 SFML 时遇到一些链接问题。我已经有了 libc++ 和 C++11 库,但是在使用字符串文字来命名窗口时,代码出现了问题。任何人以前遇到过这个并知道如何解决它?谢谢!

错误:

main.cpp

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <memory>
#include "System.hpp"

int main(int, char const**)
{
    std::shared_ptr<System> main;
    main.reset(new System());

    main->run();

    return EXIT_SUCCESS;
}

系统.hpp

#ifndef __AdventuresOfGabe__System__
#define __AdventuresOfGabe__System__

class System
{
public:
    System();
    void run();
    void runEvents();

private:
    std::shared_ptr<sf::RenderWindow> _window;

};

#endif /* defined(__AdventuresOfGabe__System__) */

System::System()
{
    _window.reset(new sf::RenderWindow(sf::VideoMode(600, 400), "Adventures of Gabe"));
    _window->setPosition(sf::Vector2i(400,400));
    _window->setFramerateLimit(60);
}

void System::run()
{
    while(_window->isOpen())
    {
        runEvents();
    }
}

void System::runEvents()
{
    sf::Event event;

    while(_window->pollEvent(event) )
    {
        if( event.type == sf::Event::Closed )
            _window->close();

        if( (event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape) )
            _window->close();
    }
}

【问题讨论】:

    标签: c++ xcode macos linker-errors sfml


    【解决方案1】:

    引用official tutorial

    关于模板设置的几句话。如果您为 C++ 编译器和标准库选择不兼容的选项,您最终会出现链接器错误。确保您遵循以下准则:

    • 如果您从下载页面下载了“GCC”版本,则应选择 C++98 with GCC and libstdc++ and target 10.5。
    • 如果您从下载页面下载了“Clang”版本,则应选择 C++11 with Clang and libc++。

    所以您需要下载其他版本的 SDK,即 «Clang» 版本。


    在不相关的注释中,您可以使用std::make_shared 来简化您的代码。

    【讨论】:

    • 工作就像一个魅力。我已经习惯了使用 GCC,所以我只是自动去下载。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-27
    • 2017-05-02
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多