Search code examples
c++visual-studio-codesfml

Why do I keep getting undefined reference using SFML in VSCode?


I'm tried compiling this program copied from the sfml tutorial

#include <SFML/Graphics.hpp>

int main()
{
    // create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "My window");

    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }

        // clear the window with black color
        window.clear(sf::Color::Black);

        // draw everything here...
        // window.draw(...);

        // end the current frame
        window.display();
    }

    return 0;
}

Using the following tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "g++",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-ID:\\Personal\\SFML\\include",
                "-LD:\\Personal\\SFML\\lib",
                "-lsfml-graphics",
                "-lsfml-window",
                "-lsfml-system",
                "-lsfml-network"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "C:\\MinGW\\bin\\g++.exe"
        }
    ]
}

But each time I did so, it keeps showing "undefined reference to" errors for each of the lines with sfml stuff.

g++ -g "D:\Prog 2\SC2\sc.cpp" -o "D:\Prog 2\SC2\sc.exe" -ID:\Personal\SFML\include -LD:\Personal\SFML\lib -lsfml-graphics -lsfml-window -lsfml-system -lsfml-network
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\pwucn\AppData\Local\Temp\ccERIoYa.o: in function `main':
D:/Prog 2/SC2/sc.cpp:6: undefined reference to `_imp___ZN2sf6StringC1EPKcRKSt6locale'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: D:/Prog 2/SC2/sc.cpp:6: undefined reference to `_imp___ZN2sf9VideoModeC1Ejjj'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: D:/Prog 2/SC2/sc.cpp:6: undefined reference to `_imp___ZN2sf12RenderWindowC1ENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: D:/Prog 2/SC2/sc.cpp:9: undefined reference to `_imp___ZNK2sf6Window6isOpenEv'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: D:/Prog 2/SC2/sc.cpp:13: undefined reference to `_imp___ZN2sf6Window9pollEventERNS_5EventE'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: D:/Prog 2/SC2/sc.cpp:17: undefined reference to `_imp___ZN2sf6Window5closeEv'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: D:/Prog 2/SC2/sc.cpp:21: undefined reference to `_imp___ZN2sf5Color5BlackE'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: D:/Prog 2/SC2/sc.cpp:21: undefined reference to `_imp___ZN2sf12RenderTarget5clearERKNS_5ColorE'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: D:/Prog 2/SC2/sc.cpp:27: undefined reference to `_imp___ZN2sf6Window7displayEv'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: D:/Prog 2/SC2/sc.cpp:6: undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: D:/Prog 2/SC2/sc.cpp:6: undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'
collect2.exe: error: ld returned 1 exit status

This keeps happening even after I included the dll files into the project as suggested from another post. Am I doing anything wrong here?


Solution

  • Turns out I got the wrong version of SFML. It works perfectly now.