0

When I try to link any SFML code I get an error.

This is my code:

#include <iostream>
#include <SFML/window.hpp>

using namespace std;
using namespace sf;

int main() {

    Window window(VideoMode(800,600),"sample window");

    while(window.isOpen()) {

        Event event;
        while(window.pollEvent(event)) {

            if (event.type == Event::Closed) {

                window.close();
            }
        }
    }

    return 0;
}

This is the error:

PS C:\Users\harsh\Documents\Programing> g++ graphics.cpp
C:\Users\harsh\AppData\Local\Temp\ccE0sN19.o:graphics.cpp:(.text+0x7c): undefined reference to `_imp___ZN2sf6StringC1EPKcRKSt6locale'
C:\Users\harsh\AppData\Local\Temp\ccE0sN19.o:graphics.cpp:(.text+0xa2): undefined reference to `_imp___ZN2sf9VideoModeC1Ejjj'
C:\Users\harsh\AppData\Local\Temp\ccE0sN19.o:graphics.cpp:(.text+0xdb): undefined reference to `_imp___ZN2sf6WindowC1ENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE'
C:\Users\harsh\AppData\Local\Temp\ccE0sN19.o:graphics.cpp:(.text+0xfe): undefined reference to `_imp___ZNK2sf6Window6isOpenEv'
C:\Users\harsh\AppData\Local\Temp\ccE0sN19.o:graphics.cpp:(.text+0x117): undefined reference to `_imp___ZN2sf6Window9pollEventERNS_5EventE'
C:\Users\harsh\AppData\Local\Temp\ccE0sN19.o:graphics.cpp:(.text+0x134): undefined reference to `_imp___ZN2sf6Window5closeEv'
C:\Users\harsh\AppData\Local\Temp\ccE0sN19.o:graphics.cpp:(.text+0x147): undefined reference to `_imp___ZN2sf6WindowD1Ev'
C:\Users\harsh\AppData\Local\Temp\ccE0sN19.o:graphics.cpp:(.text+0x17d): undefined reference to `_imp___ZN2sf6WindowD1Ev'
collect2.exe: error: ld returned 1 exit status
user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    Welcome to Stack Overflow. You might want to read [How do I format my code blocks?](https://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks). – wovano May 04 '22 at 13:45
  • Does this answer your question? [SFML: undefined reference to \_imp\_](https://stackoverflow.com/questions/48661676/sfml-undefined-reference-to-imp) – wovano May 04 '22 at 13:50
  • Show how you are linking. Do you have mingw binaries? The ones for msvc will not work. – drescherjm May 04 '22 at 14:16
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 04 '22 at 22:24
  • `g++ graphics.cpp` is not an adequate to command to compile your code. You will have to include `-l` and `-L` flags to tell the linker where to find the libraries you are using. The right options to use depend on the details of how you installed the library. – David Grayson May 05 '22 at 06:06
  • Community, it's not that hard to understand what's being asked here. OP gets a linker error and wants to know how to solve it. Question did require a bit of formatting though, which I fixed. @HarshDaniel, welcome at Stack Overflow. You might want to read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How do I format my code blocks?](https://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks) to write better questions in the future. – wovano May 05 '22 at 06:07
  • I think [this](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) is the canonical question/answer that should be the duplicate target. It seems to contain all the relevant information to find out what's the cause and how to solve it for a number of compilers/linkers. – wovano May 05 '22 at 06:10
  • OP: Just for reference, since you used the technically incorrect terms in the question: this is not a _compiler_ error but a _linker_ error. Use `-c` is you only want to compile the file (which should succeed without errors). – wovano May 05 '22 at 06:12

0 Answers0