Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Unable to compile SFML GCC project on windows - "undefined reference to..."  (Read 3974 times)

0 Members and 2 Guests are viewing this topic.

anonymous-20160130

  • Newbie
  • *
  • Posts: 3
    • View Profile
I'm trying to set up my first C++ SFML project, but whatever I do I can't seem to get the link to work.
I use the commands:
g++ -m32 -Wall -Werror -pedantic -O0 -std=c++11 -I"E:\path\to\SFML-2.3.2\include" -L"E:\path\to\SFML-2.3.2\lib" -lsfml-graphics -lsfml-window -lsfml-system  -c -o main.o main.cpp
g++ -m32 -Wall -Werror -pedantic -O0 -std=c++11 -I"E:\path\to\SFML-2.3.2\include" -L"E:\path\to\SFML-2.3.2\lib" -lsfml-graphics -lsfml-window -lsfml-system  main.o -o malsort.exe

But whenever I try it simply produces a bunch of errors I can't make any sense of:
main.o:main.cpp:(.text+0x127): undefined reference to `_imp___ZN2sf6StringC1EPKcRKSt6locale'
main.o:main.cpp:(.text+0x157): undefined reference to `_imp___ZN2sf9VideoModeC1Ejjj'
main.o:main.cpp:(.text+0x19d): undefined reference to `_imp___ZN2sf12RenderWindowC1ENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE'
main.o:main.cpp:(.text+0x1dd): undefined reference to `_imp___ZN2sf11CircleShapeC1Efj'
main.o:main.cpp:(.text+0x1f8): undefined reference to `_imp___ZN2sf5Color5GreenE'
main.o:main.cpp:(.text+0x202): undefined reference to `_imp___ZN2sf5Shape12setFillColorERKNS_5ColorE'
main.o:main.cpp:(.text+0x22c): undefined reference to `_imp___ZN2sf6Window5closeEv'
main.o:main.cpp:(.text+0x24b): undefined reference to `_imp___ZN2sf6Window9pollEventERNS_5EventE'
main.o:main.cpp:(.text+0x287): undefined reference to `_imp___ZN2sf5ColorC1Ehhhh'
main.o:main.cpp:(.text+0x2a2): undefined reference to `_imp___ZN2sf12RenderTarget5clearERKNS_5ColorE'
main.o:main.cpp:(.text+0x2bf): undefined reference to `_imp___ZN2sf12RenderStates7DefaultE'
main.o:main.cpp:(.text+0x2d3): undefined reference to `_imp___ZN2sf12RenderTarget4drawERKNS_8DrawableERKNS_12RenderStatesE'
main.o:main.cpp:(.text+0x2ef): undefined reference to `_imp___ZN2sf6Window7displayEv'
main.o:main.cpp:(.text+0x2fe): undefined reference to `_imp___ZNK2sf6Window6isOpenEv'
main.o:main.cpp:(.text+0x336): undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'
main.o:main.cpp:(.text+0x3ea): undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'
c:/tdm-gcc-32/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: main.o: bad reloc address 0x10 in section `.rdata'
c:/tdm-gcc-32/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
makefile:18: recipe for target 'all' failed
mingw32-make: *** [all] Error 1

I have no idea what to do. I'm not even trying to do any static linking or anything like that. I tried reinstalling my compiler (which, for the record, I got from http://tdm-gcc.tdragon.net/, and produces g++ (tdm-1) 4.8.1 when I use the g++ --version command), and I tried using every non-Visual Studio version of SFML available on the downloads page, I tried removing and adding the -m32 flag, I tried playing around with the -I and -L dirs and the -l flags' order, but nothing works.

My main.cpp (even though it's probably irrelevant):
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#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;
}

SpeCter

  • Full Member
  • ***
  • Posts: 151
    • View Profile
How about compiling it yourself instead of trying every incompatible version on the front page?

anonymous-20160130

  • Newbie
  • *
  • Posts: 3
    • View Profile
Well, I tried that, but it still doesn't work...

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Unable to compile SFML GCC project on windows - "undefined reference to..."
« Reply #3 on: January 31, 2016, 02:55:13 am »
main.cpp must be placed before the -lsfml-xyz
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

anonymous-20160130

  • Newbie
  • *
  • Posts: 3
    • View Profile
Oh jesus christ what a dumb fucking mistake.
Thank you, eXpl0it3r!