【问题标题】:Why g++ static linking doesn't work with sfml in windows?为什么 g++ 静态链接不适用于 Windows 中的 sfml?
【发布时间】:2021-02-17 04:02:50
【问题描述】:

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

#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

你知道我该怎么做才能成功静态编译吗?

提前谢谢你:)

【问题讨论】:

  • 尝试将-lsfml-system-s 移到编译命令的后面。一般来说,您需要将依赖项放在依赖项之前。
  • @Botje 谢谢你的回答 :) 我试过你说的,但不幸的是它没有用。

标签: c++ windows linker mingw sfml


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 2021-10-31
    • 2014-10-15
    相关资源
    最近更新 更多