在 Visual Studio 代码中调试 SFML 代码时出错

ERROR while debugging SFML code in Visual Studio Code

我在 linux 上使用 SFML 库创建了一个项目,它工作得很好。我通过启动这个脚本 exec.sh:

来启动它
g++ -c main.cpp -I/usr/include
g++ main.o -o sfml-app -L/usr/lib -lsfml-graphics -lsfml-window -lsfml-system
export LD_LIBRARY_PATH=/usr/lib && ./sfml-app

这里是 main.cpp 文件中的代码:

#include <SFML/Graphics.hpp>

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

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

但问题是我无法调试它,并且在尝试时总是出错。有人可以给我建议或解释我该怎么做吗?我需要 linux 的解决方案,我是这个 OC[=44= 的新手].

这是使用 g++ 调试后的错误:

Starting build...
Build finished with error:

/usr/bin/ld: /tmp/ccnkqRHk.o: in function `main':
/home/yashmerino/Desktop/Developing/testsfml 1 file/main.cpp:5: undefined reference to `sf::String::String(char const*, std::locale const&)'
/usr/bin/ld: /home/yashmerino/Desktop/Developing/testsfml 1 file/main.cpp:5: undefined reference to `sf::VideoMode::VideoMode(unsigned int, unsigned int, unsigned int)'
/usr/bin/ld: /home/yashmerino/Desktop/Developing/testsfml 1 file/main.cpp:5: undefined reference to `sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&)'
/usr/bin/ld: /home/yashmerino/Desktop/Developing/testsfml 1 file/main.cpp:6: undefined reference to `sf::CircleShape::CircleShape(float, unsigned long)'
/usr/bin/ld: /home/yashmerino/Desktop/Developing/testsfml 1 file/main.cpp:7: undefined reference to `sf::Color::Blue'
/usr/bin/ld: /home/yashmerino/Desktop/Developing/testsfml 1 file/main.cpp:7: undefined reference to `sf::Shape::setFillColor(sf::Color const&)'
/usr/bin/ld: /home/yashmerino/Desktop/Developing/testsfml 1 file/main.cpp:9: undefined reference to `sf::Window::isOpen() const'
/usr/bin/ld: /home/yashmerino/Desktop/Developing/testsfml 1 file/main.cpp:12: undefined reference to `sf::Window::pollEvent(sf::Event&)'
/usr/bin/ld: /home/yashmerino/Desktop/Developing/testsfml 1 file/main.cpp:15: undefined reference to `sf::Window::close()'
/usr/bin/ld: /home/yashmerino/Desktop/Developing/testsfml 1 file/main.cpp:18: undefined reference to `sf::Color::Color(unsigned char, unsigned char, unsigned char, unsigned char)'
/usr/bin/ld: /home/yashmerino/Desktop/Developing/testsfml 1 file/main.cpp:18: undefined reference to `sf::RenderTarget::clear(sf::Color const&)'
/usr/bin/ld: /home/yashmerino/Desktop/Developing/testsfml 1 file/main.cpp:19: undefined reference to `sf::RenderStates::Default'
/usr/bin/ld: /home/yashmerino/Desktop/Developing/testsfml 1 file/main.cpp:19: undefined reference to `sf::RenderTarget::draw(sf::Drawable const&, sf::RenderStates const&)'
/usr/bin/ld: /home/yashmerino/Desktop/Developing/testsfml 1 file/main.cpp:20: undefined reference to `sf::Window::display()'
/usr/bin/ld: /home/yashmerino/Desktop/Developing/testsfml 1 file/main.cpp:5: undefined reference to `sf::RenderWindow::~RenderWindow()'
/usr/bin/ld: /home/yashmerino/Desktop/Developing/testsfml 1 file/main.cpp:5: undefined reference to `sf::RenderWindow::~RenderWindow()'
/usr/bin/ld: /tmp/ccnkqRHk.o: in function `sf::CircleShape::~CircleShape()':
/usr/include/SFML/Graphics/CircleShape.hpp:41: undefined reference to `vtable for sf::CircleShape'
/usr/bin/ld: /usr/include/SFML/Graphics/CircleShape.hpp:41: undefined reference to `vtable for sf::CircleShape'
/usr/bin/ld: /usr/include/SFML/Graphics/CircleShape.hpp:41: undefined reference to `sf::Shape::~Shape()'
collect2: error: ld returned 1 exit status

尝试将库安装到您的系统路径。 对于基于 Arch Linux -

sudo pacman -S sfml

基于 Debian Linux -

sudo apt-get install sfml-dev

并且只需使用以下 g++ 命令 -

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

最好用一个IDE。您可以使用

Code Blocks
Code Lite
Apache netbeans
Eclipse

如果您熟悉 cmake,您可以使用-

Kdevelop