SFML undefined reference to impl

2019-07-23 15:40发布

问题:

I'm getting the following undefined references when using SFML for GCC 4.7 MinGW (DW2) - 32 bits:

g++ -std=c++11 -Wall -I$(SFML_INCLUDE) -o pong main.o

main.o:main.cpp: undefined reference to `_imp___ZN2sf6StringC1EPKc
RKSt6locale'
main.o:main.cpp: undefined reference to `_imp___ZN2sf9VideoModeC1E
jjj'
main.o:main.cpp: undefined reference to `_imp___ZN2sf12RenderWindo
wC1ENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE'
main.o:main.cpp: undefined reference to `_imp___ZN2sf11CircleShape
C1Efj'
....
....
....
collect2.exe: error: ld returned 1 exit status
make: *** [all] Error 1

There are many more than this. This is how I am linking:

LIBRARIES = -lsfml-graphics -lsfml-window -lglu32 -lopengl32 -lglew32
g++ -c include/main.cpp -L/lib  $(LIBRARIES)

I'm on Windows. How do I get rid of these undefined references?

EDIT: This is the program:

#define SFML_STATIC

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

回答1:

You have compiling and linking mixed up. This is linking:

g++ -std=c++11 -Wall -I$(SFML_INCLUDE) -o pong main.o

And this is compiling:

g++ -c include/main.cpp -L/lib  $(LIBRARIES)

The linking, not the compiling, needs the $(LIBRARIES) variable and the -L option.

g++ main.o -o pong -L/lib $(LIBRARIES)

And the compiling needs the -std=c++11 and the -Wall and the -I.

g++ -std=c++11 -Wall -c -I$(SFML_INCLUDE) include/main.cpp


标签: c++ windows sfml