带有静态库错误的 SFML 编译。 C++

SFML Compilation with static libs Error. C++

正在尝试编译我愚蠢的 hello-world SFML 项目进行测试。但是从链接器收到奇怪的错误消息。 (当我使用共享库编译时 - 一切正常。)

g++ -DSFML_STATIC -O2 -no-pie -fno-pie -o main main.cpp -L/home/uzzer/LIB/SFML-2.5.1/lib -lstdc++ -lc -lm -ldl -lX11 -lGL -lGLU -lsfml-system-s -lsfml-graphics-s -lsfml-window-s

错误信息:

/usr/bin/ld: /home/uzzer/LIB/SFML-2.5.1/lib/libsfml-graphics-s.a(RenderTarget.cpp.o): in function `(anonymous namespace)::equationToGlConstant(sf::BlendMode::Equation) [clone .part.0]':
RenderTarget.cpp:(.text+0x8): undefined reference to `sf::err()'
/usr/bin/ld: /home/uzzer/LIB/SFML-2.5.1/lib/libsfml-graphics-s.a(RenderTarget.cpp.o): in function `(anonymous namespace)::factorToGlConstant(sf::BlendMode::Factor) [clone .part.0]':
RenderTarget.cpp:(.text+0xa8): undefined reference to `sf::err()'
/usr/bin/ld: /home/uzzer/LIB/SFML-2.5.1/lib/libsfml-graphics-s.a(RenderTarget.cpp.o): in function `sf::RenderTarget::clear(sf::Color const&)':
RenderTarget.cpp:(.text+0x2c9): undefined reference to `glClearColor'
/usr/bin/ld: /home/uzzer/LIB/SFML-2.5.1/lib/libsfml-graphics-s.a(RenderTarget.cpp.o): in function `sf::RenderTarget::popGLStates()':
RenderTarget.cpp:(.text+0x854): undefined reference to `glMatrixMode'
/usr/bin/ld: RenderTarget.cpp:(.text+0x859): undefined reference to `glPopMatrix'
/usr/bin/ld: RenderTarget.cpp:(.text+0x863): undefined reference to `glMatrixMode'
/usr/bin/ld: RenderTarget.cpp:(.text+0x868): undefined reference to `glPopMatrix'
/usr/bin/ld: RenderTarget.cpp:(.text+0x872): undefined reference to `glMatrixMode'
/usr/bin/ld: RenderTarget.cpp:(.text+0x877): undefined reference to `glPopMatrix'
/usr/bin/ld: RenderTarget.cpp:(.text+0x87c): undefined reference to `glPopClientAttrib'
/usr/bin/ld: /home/uzzer/LIB/SFML-2.5.1/lib/libsfml-graphics-s.a(RenderTarget.cpp.o): in function `sf::RenderTarget::initialize()':
RenderTarget.cpp:(.text+0x9c3): undefined reference to `sf::Lock::Lock(sf::Mutex&)'
/usr/bin/ld: RenderTarget.cpp:(.text+0x9de): undefined reference to `sf::Lock::~Lock()'
/usr/bin/ld: /home/uzzer/LIB/SFML-2.5.1/lib/libsfml-graphics-s.a(RenderTarget.cpp.o): in function `sf::RenderTarget::applyCurrentView()':
RenderTarget.cpp:(.text+0xa60): undefined reference to `glViewport'
/usr/bin/ld: RenderTarget.cpp:(.text+0xa6a): undefined reference to `glMatrixMode'
/usr/bin/ld: RenderTarget.cpp:(.text+0xa82): undefined reference to `glLoadMatrixf'
/usr/bin/ld: RenderTarget.cpp:(.text+0xa8c): undefined reference to `glMatrixMode'
/usr/bin/ld: /home/uzzer/LIB/SFML-2.5.1/lib/libsfml-graphics-s.a(RenderTarget.cpp.o): in function `sf::RenderTarget::applyBlendMode(sf::BlendMode const&)':
RenderTarget.cpp:(.text+0xbb0): undefined reference to `glBlendFunc'
/usr/bin/ld: RenderTarget.cpp:(.text+0xc19): undefined reference to `sf::err()'
/usr/bin/ld: RenderTarget.cpp:(.text+0xc6e): undefined reference to `sf::err()'
/usr/bin/ld: RenderTarget.cpp:(.text+0xcc3): undefined reference to `sf::err()'
/usr/bin/ld: /home/uzzer/LIB/SFML-2.5.1/lib/libsfml-graphics-s.a(RenderTarget.cpp.o): in function `sf::RenderTarget::resetGLStates()':
RenderTarget.cpp:(.text+0xf80): undefined reference to `glDisable'
/usr/bin/ld: RenderTarget.cpp:(.text+0xf8a): undefined reference to `glDisable'
/usr/bin/ld: RenderTarget.cpp:(.text+0xf94): undefined reference to `glDisable'
/usr/bin/ld: RenderTarget.cpp:(.text+0xf9e): undefined reference to `glDisable'
/usr/bin/ld: RenderTarget.cpp:(.text+0xfa8): undefined reference to `glEnable'
/usr/bin/ld: RenderTarget.cpp:(.text+0xfb2): undefined reference to `glEnable'
/usr/bin/ld: RenderTarget.cpp:(.text+0xfbc): undefined reference to `glMatrixMode'
/usr/bin/ld: RenderTarget.cpp:(.text+0xfc1): undefined reference to `glLoadIdentity'
/usr/bin/ld: RenderTarget.cpp:(.text+0xfcb): undefined reference to `glEnableClientState'
/usr/bin/ld: RenderTarget.cpp:(.text+0xfd5): undefined reference to `glEnableClientState'
/usr/bin/ld: RenderTarget.cpp:(.text+0xfdf): undefined reference to `glEnableClientState'
/usr/bin/ld: /home/uzzer/LIB/SFML-2.5.1/lib/libsfml-graphics-s.a(RenderTarget.cpp.o): in function `sf::RenderTarget::pushGLStates()':
RenderTarget.cpp:(.text+0x1104): undefined reference to `glPushClientAttrib'
/usr/bin/ld: RenderTarget.cpp:(.text+0x110e): undefined reference to `glPushAttrib'
/usr/bin/ld: RenderTarget.cpp:(.text+0x1118): undefined reference to `glMatrixMode'
/usr/bin/ld: RenderTarget.cpp:(.text+0x111d): undefined reference to `glPushMatrix'
/usr/bin/ld: RenderTarget.cpp:(.text+0x1127): undefined reference to `glMatrixMode'
/usr/bin/ld: RenderTarget.cpp:(.text+0x112c): undefined reference to `glPushMatrix'
/usr/bin/ld: RenderTarget.cpp:(.text+0x1136): undefined reference to `glMatrixMode'
/usr/bin/ld: RenderTarget.cpp:(.text+0x113b): undefined reference to `glPushMatrix'
/usr/bin/ld: /home/uzzer/LIB/SFML-2.5.1/lib/libsfml-graphics-s.a(RenderTarget.cpp.o): in function `sf::RenderTarget::setupDraw(bool, sf::RenderStates const&)':

............等等

/usr/bin/ld: GLExtensions.cpp:(.text+0xa1): undefined reference to `sf::err()'
/usr/bin/ld: GLExtensions.cpp:(.text+0xf8): undefined reference to `sf::err()'
/usr/bin/ld: GLExtensions.cpp:(.text+0x15e): undefined reference to `glGetString'
/usr/bin/ld: /home/uzzer/LIB/SFML-2.5.1/lib/libsfml-graphics-s.a(Image.cpp.o): in function `sf::Image::getPixelsPtr() const':
Image.cpp:(.text+0x778): undefined reference to `sf::err()'
collect2: error: ld returned 1 exit status

gcc 版本 10.2.0 (Ubuntu 10.2.0-13ubuntu1).

$g++ -v -print-search-dirs 
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/10/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa:hsa
OFFLOAD_TARGET_DEFAULT=1
install: /usr/lib/gcc/x86_64-linux-gnu/10/
programs: =/usr/lib/gcc/x86_64-linux-gnu/10/:/usr/lib/gcc/x86_64-linux-gnu/10/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/10/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/bin/x86_64-linux-gnu/10/:/usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/bin/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/bin/
libraries: =/usr/lib/gcc/x86_64-linux-gnu/10/:/usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/lib/x86_64-linux-gnu/10/:/usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/lib/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/10/:/usr/lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/10/../../../../lib/:/lib/x86_64-linux-gnu/10/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/10/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/lib/:/usr/lib/gcc/x86_64-linux-gnu/10/../../../:/lib/:/usr/lib/

我做错了什么/愚蠢?

库在命令行上的顺序很重要 - 这意味着包含函数定义的库应该出现在任何使用它的源文件或目标文件之后。请参阅 here 了解更多信息。 @SeanFrancisNBallais 的评论也正是关于这一点。

在您的情况下,您需要将 sfml-system-s 库放在所有其他 SFML 库之后,如下所示:

 g++ <...other options...> -lsfml-graphics-s -lsfml-window-s -lsfml-system-s -lGLU -lGL -lX11 -ludev -lpthread

此外,从这一行您可以看到 GL 库使用 X11 库(反之亦然)等等。一些基本的 GCC 库,例如 stdc++m,不会出现在命令行中 - 它们默认出现在此处。