问题
I'm trying to create a C++ application with SFML. Followed the tutorial, installed MinGW for Windows. My project has a main.cpp file in its root folder, and SFML is included in lib/sfml (relative to main.cpp).
I can compile without problems with the command g++ -c main.cpp -g -o build/debug/game.o -Ilib/sfml/include
But I get undefined references when I try to link wit the command g++ build/debug/game.o -o build/debug/game.exe -Llib/sfml/lib -lsfml-graphics-d -lsfml-window-d -lsfml-system-d
Some of the errors:
C:\Users\andre\Documents\Repos\Game/main.cpp:5: undefined reference to `_imp___ZN2sf6StringC1EPKcRKSt6locale'
C:\Users\andre\Documents\Repos\Game/main.cpp:5: undefined reference to `_imp___ZN2sf9VideoModeC1Ejjj'
C:\Users\andre\Documents\Repos\Game/main.cpp:5: undefined reference to `_imp___ZN2sf12RenderWindowC1ENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE'
I've searched already and nothing seems to help me. What I found strange is that in some previous versions of SFML it came with more libs inside the lib folder, like opengl32.lib. Still, it says that, for example, sfml-graphics I need:
sfml-window-s.lib
sfml-system-s.lib
opengl32.lib
freetype.lib
jpeg.lib
The only one missing is opengl32.lib. If I consider sfml-system and sfml-window, it is missing too winmm.lib, gdi32.lib. I don't know if this has something to do with the errors, but any help is welcome.
回答1:
Solved, my mistake. I downloaded some days ago the SFML version for Visual C++ compiler, but then I gave up on Visual Studio 2017 and decided to use Visual Studio Code with GCC. So I tried to use the SFML I had thinking it was the right one. Now I've downloaded the right version and it is working perfectly.
回答2:
If you're linking the static version of SFML, SFML's header files have to know about this (since the function signature changes slightly). As such you'll have to define SFML_STATIC
somewhere before you include any SFML header, ideally as part of your project, make files, or build system.
In addition, you'll have to make sure to link the proper versions of the libraries and those you actually want to use.
As an example for the system library:
sfml-system
is the shared release build of the library.sfml-system-d
is the shared debug build of the library.sfml-system-s
is the static release build of the library.sfml-system-s-d
is the static debug build of the library.
You'll only have to link SFML's dependencies (such as OpenGL), if you're linking the static version of SFML.
SFML never shipped with OpenGL, you're probably confusing it with OpenAL-soft. Either way, the additional library files you're missing are either provided by your toolchain/system (like OpenGL or mmsystem) or can be found precompiled in SFML's extlibs/libs folder (depending on whether you've downloaded or compiled SFML yourself).
来源:https://stackoverflow.com/questions/48661676/sfml-undefined-reference-to-imp