为什么 g++ 静态链接不适用于 windows 中的 sfml?

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

我正在尝试静态编译一个简单的程序:

#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++ 编译器标志:

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

这是 g++ 编译器产生的错误示例:

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

(相同的输出错误: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 和 JoystickImpl.cpp.obj)

我用的是mingw版本x86_64-8.1.0-posix-seh-rt_v6-rev0 和 sfml 版本 windows-gcc-810-mingw-64

你知道怎样做才能成功静态编译吗?

提前谢谢你:)

您没有按正确的顺序传递参数。

Graphics
Window
Audio
Network
System

这是正确的顺序,这是正确的命令。

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