我正在尝试让SFML2.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
我做错什么了?错误表示未定义的引用,这意味着存在某种东西(库?)那是找不到的。
SFML2.1包附带了调试和发布库(Libsfml),这与此有什么关系吗?
发布于 2015-07-24 14:37:49
整个需求在F.A.Q.上进行了描述。
-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
。
发布于 2014-07-04 13:54:42
首先要针对动态库进行链接,因此通过将-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
至少这样,您将链接到发布的静态库。
https://stackoverflow.com/questions/20934968
复制相似问题