Search code examples
c++windowslinkermingwsfml

Why g++ static linking doesn't work with sfml in windows?


I am trying to compile a simple program statically:

#include <iostream>
#include <vector>
#include <string>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

using namespace std;

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "HW Test");
    vector<string> m = {"Hello", "world"};
    for (const string &msg : m) {
        cout << msg;
        cout << " ";
    }
}

g++ compiler flags:

C:\Users\Retsal\Documents\Cpp\SfmlTest>g++ -o hw.exe .\src\helloworld.cpp -I C:\Users\Retsal\Documents\Cpp\SfmlTest\include -DSFML_STATIC -L C:\Users\Retsal\Documents\Cpp\SfmlTest\lib -l sfml-system-s -l sfml-graphics-s -l sfml-window-s -l gdi32 -l winmm -l opengl32 -l freetype

and this is a sample of the error produced by g++ compiler:

C:\Users\Retsal\Documents\Cpp\SfmlTest\lib/libsfml-graphics-s.a(Texture.cpp.obj):Texture.cpp:(.text+0x5c): undefined reference to `sf::Lock::Lock(sf::Mutex&)'
C:\Users\Retsal\Documents\Cpp\SfmlTest\lib/libsfml-graphics-s.a(Texture.cpp.obj):Texture.cpp:(.text+0x76): undefined reference to `sf::Lock::~Lock()'
C:\Users\Retsal\Documents\Cpp\SfmlTest\lib/libsfml-graphics-s.a(Texture.cpp.obj):Texture.cpp:(.text+0x16f): undefined reference to `sf::Lock::Lock(sf::Mutex&)'

C:\Users\Retsal\Documents\Cpp\SfmlTest\lib/libsfml-window-s.a(JoystickImpl.cpp.obj):JoystickImpl.cpp:(.text.startup+0x5f): undefined reference to `sf::milliseconds(int)'
C:\Users\Retsal\Documents\Cpp\SfmlTest\lib/libsfml-window-s.a(JoystickImpl.cpp.obj):JoystickImpl.cpp:(.text.startup+0x7c): undefined reference to `sf::Clock::Clock()'
collect2.exe: error: ld returned 1 exit status

(Same output error for: Texture.cpp.obj, RenderTextureImplFBO.cpp.obj, GLExtensions.cpp.obj, Image.cpp.obj, Shader.cpp.obj, VertexBuffer.cpp.obj, Context.cpp.obj, Window.cpp.obj, WindowBase.cpp.obj, WindowImpl.cpp.obj, WglContext.cpp.obj, WindowImplWin32.cpp.obj and JoystickImpl.cpp.obj)

I am using mingw version x86_64-8.1.0-posix-seh-rt_v6-rev0 and sfml version windows-gcc-810-mingw-64

Do you know what can I do to successfully statically compile?

Thank you in advance :)


Solution

  • You have not passed parameters in correct order .

    Graphics
    Window
    Audio
    Network
    System
    

    This is correct order and this is correct command.

    C:\Users\Retsal\Documents\Cpp\SfmlTest>g++ -o hw.exe .\src\helloworld.cpp -I C:\Users\Retsal\Documents\Cpp\SfmlTest\include -DSFML_STATIC -L C:\Users\Retsal\Documents\Cpp\SfmlTest\lib -l sfml-graphics-s -l sfml-window-s -l sfml-system-s  -l gdi32 -l winmm -l opengl32 -l freetype