SFML community forums

Help => General => Topic started by: anonymous-20160130 on January 30, 2016, 03:37:27 pm

Title: Unable to compile SFML GCC project on windows - "undefined reference to..."
Post by: anonymous-20160130 on January 30, 2016, 03:37:27 pm
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 (http://www.sfml-dev.org/download/sfml/2.3.2/) 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;
}
Title: Re: Unable to compile SFML GCC project on windows - "undefined reference to..."
Post by: SpeCter on January 30, 2016, 04:05:57 pm
How about compiling it yourself instead of trying every incompatible version on the front page?
Title: Re: Unable to compile SFML GCC project on windows - "undefined reference to..."
Post by: anonymous-20160130 on January 30, 2016, 11:45:28 pm
Well, I tried that, but it still doesn't work...
Title: Unable to compile SFML GCC project on windows - "undefined reference to..."
Post by: eXpl0it3r on January 31, 2016, 02:55:13 am
main.cpp must be placed before the -lsfml-xyz
Title: Re: Unable to compile SFML GCC project on windows - "undefined reference to..."
Post by: anonymous-20160130 on January 31, 2016, 01:39:11 pm
Oh jesus christ what a dumb fucking mistake.
Thank you, eXpl0it3r!