【问题标题】:MingW static library linking - SFML 2.1MingW 静态库链接 - SFML 2.1
【发布时间】:2014-01-05 14:53:39
【问题描述】:

我正在尝试让 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 的静态库。
  • 我尝试下载不同版本的 SFML 2.1,现在它可以编译(耶!:D),但程序无法运行,我收到错误:0xc000007b。该库是 32 位的,但我的操作系统是 64 位的。这有关系吗?
  • 不应该; x86-64 与 x86 兼容(或者您的许多程序无法运行)不过,您可能应该确保 您的 代码也被编译为 32 位。
  • 该错误意味着它找不到某些共享库(Windows 下的.dll)。见stackoverflow.com/questions/10492037/…
  • 调试/发布可能与动态与静态运行时有关。在 Windows 上,调试和发布运行时相互不兼容,您必须在 编译 时进行选择。您还必须选择是在编译时链接静态库还是共享库(需要特殊定义)。不幸的是,我不记得 MinGW 上的特定选项组合。

标签: c++ mingw undefined-reference


【解决方案1】:

F.A.Q. 上描述了整个要求:

  1. 添加-DSFML_STATIC预处理器标志,

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

  3. 添加所有需要的静态库:-lopengl32 -lwinmm -lgdi32 是使用图形、窗口和系统模块时的最低要求。

一个简单的 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

要删除控制台,请将-mwindows 链接器标志添加到LDFLAGS

【讨论】:

    【解决方案2】:

    您首先要链接动态库,因此通过在库中添加 -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
    

    至少你会链接到发布静态库。

    【讨论】:

      猜你喜欢
      • 2014-05-05
      • 2011-07-14
      • 2014-10-15
      • 2016-10-19
      • 1970-01-01
      • 2018-12-03
      • 2012-09-22
      • 2020-11-04
      • 1970-01-01
      相关资源
      最近更新 更多