MingW static library linking - SFML 2.1
Asked Answered
M

2

6

i'm trying to get SFML 2.1 to work with MingW, but it's causing problems.

my compile line in the MingW compiler is:

g++ -ID:\SFML-2.1\include -LD:\SFML-2.1\lib main.cpp -lsfml-graphics -lsfml-window -lsfml-system

i'm trying to link .a files (does this mean that i should add soemthing tot eh compile line?).

the code is as follows:

#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::White);

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

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

return 0;
}

when i try to compile i get these errors:

$ make main
g++ -ID:\SFML-2.1\include -LD:\SFML-2.1\lib main.cpp -lsfml-graphics -lsfml-wind
ow -lsfml-system
C:\Users\Junker\AppData\Local\Temp\ccapaUOM.o:main.cpp:(.text+0xe5): undefined r
eference to `_imp___ZN2sf6StringC1EPKcRKSt6locale'
C:\Users\Junker\AppData\Local\Temp\ccapaUOM.o:main.cpp:(.text+0x10b): undefined
reference to `_imp___ZN2sf9VideoModeC1Ejjj'
C:\Users\Junker\AppData\Local\Temp\ccapaUOM.o:main.cpp:(.text+0x147): undefined
reference to `_imp___ZN2sf12RenderWindowC1ENS_9VideoModeERKNS_6StringEjRKNS_15Co
ntextSettingsE'
C:\Users\Junker\AppData\Local\Temp\ccapaUOM.o:main.cpp:(.text+0x178): undefined
reference to `_imp___ZN2sf6Window5closeEv'
C:\Users\Junker\AppData\Local\Temp\ccapaUOM.o:main.cpp:(.text+0x18d): undefined
reference to `_imp___ZN2sf6Window9pollEventERNS_5EventE'
C:\Users\Junker\AppData\Local\Temp\ccapaUOM.o:main.cpp:(.text+0x1a4): undefined
reference to `_imp___ZN2sf5Color5WhiteE'
C:\Users\Junker\AppData\Local\Temp\ccapaUOM.o:main.cpp:(.text+0x1ae): undefined
reference to `_imp___ZN2sf12RenderTarget5clearERKNS_5ColorE'
C:\Users\Junker\AppData\Local\Temp\ccapaUOM.o:main.cpp:(.text+0x1c0): undefined
reference to `_imp___ZN2sf6Window7displayEv'
C:\Users\Junker\AppData\Local\Temp\ccapaUOM.o:main.cpp:(.text+0x1cf): undefined
reference to `_imp___ZNK2sf6Window6isOpenEv'
C:\Users\Junker\AppData\Local\Temp\ccapaUOM.o:main.cpp:(.text+0x1e7): undefined
reference to `_imp___ZN2sf12RenderWindowD1Ev'
C:\Users\Junker\AppData\Local\Temp\ccapaUOM.o:main.cpp:(.text+0x20e): undefined
reference to `_imp___ZN2sf12RenderWindowD1Ev'
C:\Users\Junker\AppData\Local\Temp\ccapaUOM.o:main.cpp:(.text+0x235): undefined
reference to `_imp___ZN2sf12RenderWindowD1Ev'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\J
unker\AppData\Local\Temp\ccapaUOM.o: bad reloc address 0xf in section `.text$_ZN
2sf6StringD1Ev[__ZN2sf6StringD1Ev]'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link
failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
make: *** [main] Error 1

what am i doing wrong? the errors say undefined reference, which means that there is something (a library?) that is can't find.

The SFML 2.1 package came with both debug and release libraries (libsfml--s-d) does this have something to do with it?

Moy answered 5/1, 2014 at 14:53 Comment(6)
Strange; your invocation looks right to me. Try using the static libraries with -DSFML_STATIC.Adenitis
i've tried to download a different version of SFML 2.1 and now it compiles(yay! :D), but the program can't run i get an error: 0xc000007b. the library is 32 bit, but my operating system is 64 bit. does this matter?Moy
It shouldn't; x86-64 is compatible with x86 (or many of your programs wouldn't work) You should probably ensure that your code is compiled as 32-bit too, though.Adenitis
That error means it can't find some shared library (.dll under windows). See #10492537Saidel
Debug/release may have something to do with this as may dynamic vs. static runtime. On windows the debug and release runtime are mutually incompatible and you have to choose when compiling. You also have to choose whether you will be linking static or shared libraries at compile time (special definitions needed). Unfortunately I don't remember the particular option combinations on MinGW.Saidel
i got it to work, it was a .dll problem, along with using a wrong version of mingw :DMoy
C
4

The whole requirements are described on the F.A.Q.:

  1. Add the -DSFML_STATIC preprocessor flag,

  2. Use the library files suffixed with -s: -lsfml-graphics-s instead of -lsfml-graphics,

  3. Add every needed static library: -lopengl32 -lwinmm -lgdi32 is a minimum when using the Graphics, Window and System modules.

A simple Makefile would looks like this:

EXE := static.exe
SRC := $(wildcard *.cpp)
OBJ := $(SRC:.cpp=.o)
DEP := $(OBJ:.o=.d)

CPPFLAGS := -Ipath/to/SFML/include -MMD -MP -DSFML_STATIC
CXXFLAGS := -std=c++11 -Wall -W -pedantic
LDFLAGS  := -Lpath/to/SFML/lib
LDLIBS   := -lsfml-graphics-s -lsfml-window-s -lsfml-system-s
LDLIBS   += -lopengl32 -lwinmm -lgdi32

.PHONY: all clean

all: $(EXE)

$(EXE): $(OBJ)
    $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@

clean:
    $(RM) $(EXE) $(OBJ) $(DEP)

ifeq "$(MAKECMDGOALS)" ""
-include $(DEP)
endif

To remove the console, add the -mwindows linker flag to LDFLAGS.

Continuator answered 24/7, 2015 at 14:37 Comment(0)
A
3

You are linking against the dynamic libraries for a start, so change to the static libraries by adding the -s suffix to the libraries.

You'll also need to add the SFML_STATIC pre-processor define too

g++ -DSFML_STATIC -ID:\SFML-2.1\include -LD:\SFML-2.1\lib main.cpp -lsfml-graphics-s -lsfml-window-s -lsfml-system-s

At least then you'll be linking against the release static library.

Allout answered 4/7, 2014 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.