MingW 静态库链接 - SFML 2.1.
MingW static library linking - SFML 2.1
我正在尝试让SFML 2.1与MingW一起工作,但它引起了问题。
我在 MingW 编译器中的编译行是:
g++ -ID:SFML-2.1include -LD:SFML-2.1lib main.cpp -lsfml-graphics -lsfml-window -lsfml-system
我正在尝试链接 .a 文件(这是否意味着我应该添加 soemthing tot eh 编译行?
代码如下:
#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.1include -LD:SFML-2.1lib main.cpp -lsfml-graphics -lsfml-wind
ow -lsfml-system
C:UsersJunkerAppDataLocalTempccapaUOM.o:main.cpp:(.text+0xe5): undefined r
eference to `_imp___ZN2sf6StringC1EPKcRKSt6locale'
C:UsersJunkerAppDataLocalTempccapaUOM.o:main.cpp:(.text+0x10b): undefined
reference to `_imp___ZN2sf9VideoModeC1Ejjj'
C:UsersJunkerAppDataLocalTempccapaUOM.o:main.cpp:(.text+0x147): undefined
reference to `_imp___ZN2sf12RenderWindowC1ENS_9VideoModeERKNS_6StringEjRKNS_15Co
ntextSettingsE'
C:UsersJunkerAppDataLocalTempccapaUOM.o:main.cpp:(.text+0x178): undefined
reference to `_imp___ZN2sf6Window5closeEv'
C:UsersJunkerAppDataLocalTempccapaUOM.o:main.cpp:(.text+0x18d): undefined
reference to `_imp___ZN2sf6Window9pollEventERNS_5EventE'
C:UsersJunkerAppDataLocalTempccapaUOM.o:main.cpp:(.text+0x1a4): undefined
reference to `_imp___ZN2sf5Color5WhiteE'
C:UsersJunkerAppDataLocalTempccapaUOM.o:main.cpp:(.text+0x1ae): undefined
reference to `_imp___ZN2sf12RenderTarget5clearERKNS_5ColorE'
C:UsersJunkerAppDataLocalTempccapaUOM.o:main.cpp:(.text+0x1c0): undefined
reference to `_imp___ZN2sf6Window7displayEv'
C:UsersJunkerAppDataLocalTempccapaUOM.o:main.cpp:(.text+0x1cf): undefined
reference to `_imp___ZNK2sf6Window6isOpenEv'
C:UsersJunkerAppDataLocalTempccapaUOM.o:main.cpp:(.text+0x1e7): undefined
reference to `_imp___ZN2sf12RenderWindowD1Ev'
C:UsersJunkerAppDataLocalTempccapaUOM.o:main.cpp:(.text+0x20e): undefined
reference to `_imp___ZN2sf12RenderWindowD1Ev'
C:UsersJunkerAppDataLocalTempccapaUOM.o:main.cpp:(.text+0x235): undefined
reference to `_imp___ZN2sf12RenderWindowD1Ev'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:UsersJ
unkerAppDataLocalTempccapaUOM.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
预处理器标志, -
使用后缀为
-s
:-lsfml-graphics-s
的库文件,而不是-lsfml-graphics
, -
添加每个需要的静态库:使用图形、窗口和系统模块时,
-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
。
首先,
您正在链接动态库,因此请通过向库添加 -s 后缀来更改为静态库。
您还需要添加SFML_STATIC预处理器定义
g++ -DSFML_STATIC -ID:SFML-2.1include -LD:SFML-2.1lib main.cpp -lsfml-graphics-s -lsfml-window-s -lsfml-system-s
至少那时您将链接到发布静态库。
相关文章:
- 尝试将 sfml 和 c++ 与 Windows 10 上的可移植 vscode 链接起来
- 静态链接 Visual Studio dll 到动态链接的 sfml 项目
- Cmake 在构建 sfml 时不链接
- 有没有办法在VSCode Mac中链接SFML库
- 使用静态SFML库时无法链接我的项目
- (Visual Studio)尝试用SFML库静态链接microsoft库时出现大量链接器错误
- MingW 静态库链接 - SFML 2.1.
- SFML 网络错误:链接时未定义的引用
- 过程入口点_ZSt24__throw_out_of_range_fmtPKcz不能位于动态链接库 sfml-graphi
- 在 Codelite 中链接 SFML,找不到库
- 使用 g++ 在 64 位 Win7 上链接 SFML-2.2
- 在 CodeLite 中链接 SFML
- C++链接sfml音频时未定义的引用
- C++ SFML游戏编程链接错误
- SFML 2.1代码::Blocks静态链接不工作
- 由于SFML框架,OS X上的链接器错误
- Visual Studio链接器在链接SFML-2.1时出错
- SFML 和 VS2013 - WinMain/main 链接器错误
- SFML链接器错误
- XCode和c++ 11的SFML链接错误