用 MingW 静态库链接 SFML 2.1

6
我正在尝试让SFML 2.1与MingW一起工作,但是它引起了问题。
我的MingW编译器中的编译行是:
g++ -ID:\SFML-2.1\include -LD:\SFML-2.1\lib main.cpp -lsfml-graphics -lsfml-window -lsfml-system

我正在尝试链接.a文件(这是否意味着我应该在编译行中添加一些内容?)。
代码如下:
#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;
}

当我尝试编译时,出现以下错误:
$ 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

我做错了什么?错误提示说“未定义的引用”,这意味着有些东西(一个库?)找不到。
SFML 2.1套件附带了调试和发布库(libsfml--s-d),这是否与此有关?

奇怪,你的调用看起来没问题。尝试使用静态库和 -DSFML_STATIC - Lightness Races in Orbit
我尝试下载了一个不同版本的SFML 2.1,现在它可以编译(耶!:D),但程序无法运行,出现错误:0xc000007b。该库是32位的,但我的操作系统是64位的。这会有影响吗? - user3149569
不应该有问题;x86-64与x86兼容(否则你的许多程序将无法运行)。但是,您可能应该确保您的代码也编译为32位。 - Lightness Races in Orbit
这个错误意味着它找不到某些共享库(在Windows下是.dll)。请参见https://dev59.com/xWkv5IYBdhLWcg3wlRz1。 - Jan Hudec
调试/发布可能与此有关,动态 vs 静态运行时也可能有关。在 Windows 上,调试和发布运行时是相互不兼容的,您必须在编译时进行选择。您还必须选择在编译时链接静态库还是共享库(需要特殊定义)。不幸的是,我不记得 MinGW 上的特定选项组合。 - Jan Hudec
我解决了,原来是.dll问题,再加上使用了错误版本的mingw :D - user3149569
2个回答

3

您正在链接到动态库,因此请通过向库添加-s后缀来更改为静态库。

您还需要添加SFML_STATIC预处理器定义。

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

至少这样你就可以链接到发布的静态库了。


3
整个要求在F.A.Q.中有详细描述:
  1. 添加-DSFML_STATIC预处理器标志,

  2. 使用后缀为-s的库文件:使用-lsfml-graphics-s而不是-lsfml-graphics

  3. 添加所有需要的静态库:-lopengl32 -lwinmm -lgdi32在使用Graphics、Window和System模块时至少需要这些。

一个简单的Makefile如下所示:
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

要移除控制台,请在LDFLAGS中添加-mwindows链接器标志。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接