C++ SFML编译时出现静态libs错误。C++;

C++ SFML编译时出现静态libs错误。C++;,c++,sfml,C++,Sfml,试图编译我愚蠢的hello world SFML项目进行测试。但是从链接器获取奇怪的错误消息。(当我使用共享库编译时,一切都正常。) 错误消息: /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 .p

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

错误消息:

/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)


我做错了什么?

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

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

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

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

尝试
g++-DSFML\u STATIC-O2-no pie-fno pie-o main.cpp-L/home/uzzer/LIB/SFML-2.5.1/LIB-lsfml-system-s-lsfml-graphics-s-lsfml-window-s-lstdc-lc-lm-ldl-11-lGL-lGLU
。会发生什么?会发生这样的事:你太棒了。非常感谢。
$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/
 g++ <...other options...> -lsfml-graphics-s -lsfml-window-s -lsfml-system-s -lGLU -lGL -lX11 -ludev -lpthread