为什么即使我的系统有 `/usr/lib/libglfw.so.3` 文件,`ld` 也找不到 `glfw3`?

Why can't `ld` find `glfw3` even though my system has `/usr/lib/libglfw.so.3` file?

我正在尝试使用以下方法构建文件:

$ g++ tutorial.cpp   -lGL -lGLEW -lglfw3 -lm   -o tutorial

并得到:

/usr/bin/ld: cannot find -lglfw3
collect2: error: ld returned 1 exit status

我检查了包,文件已经存在了:

$ pacman -Ql glfw-x11
glfw-x11 /usr/
glfw-x11 /usr/include/
glfw-x11 /usr/include/GLFW/
glfw-x11 /usr/include/GLFW/glfw3.h
glfw-x11 /usr/include/GLFW/glfw3native.h
glfw-x11 /usr/lib/
glfw-x11 /usr/lib/cmake/
glfw-x11 /usr/lib/cmake/glfw3/
glfw-x11 /usr/lib/cmake/glfw3/glfw3Config.cmake
glfw-x11 /usr/lib/cmake/glfw3/glfw3ConfigVersion.cmake
glfw-x11 /usr/lib/cmake/glfw3/glfw3Targets-noconfig.cmake
glfw-x11 /usr/lib/cmake/glfw3/glfw3Targets.cmake
glfw-x11 /usr/lib/libglfw.so
glfw-x11 /usr/lib/libglfw.so.3
glfw-x11 /usr/lib/libglfw.so.3.3
glfw-x11 /usr/lib/pkgconfig/
glfw-x11 /usr/lib/pkgconfig/glfw3.pc
glfw-x11 /usr/share/
glfw-x11 /usr/share/licenses/
glfw-x11 /usr/share/licenses/glfw-x11/
glfw-x11 /usr/share/licenses/glfw-x11/LICENSE.md

我在 this question(关于其他库)上看到了制作文件 link 的说明:

The problem is the linker is looking for libmagic.so but you only have libmagic.so.1

A quick hack is to symlink libmagic.so.1 to libmagic.so

我查了一下,我的系统确实有:

$ ls -l /usr/lib/libglfw*
lrwxrwxrwx 1 root root     12 abr 13 12:04 /usr/lib/libglfw.so -> libglfw.so.3
lrwxrwxrwx 1 root root     14 abr 13 12:04 /usr/lib/libglfw.so.3 -> libglfw.so.3.3
-rwxr-xr-x 1 root root 285504 abr 13 12:04 /usr/lib/libglfw.so.3.3

作为另一个答案的锯子:

As just formulated by grepsedawk, the answer lies in the -l option of g++, calling ld. If you look at the man page of this command, you can either do:

  • g++ -l:libmagic.so.1 [...]

我试过了,确实有效:

$ g++ tutorial.cpp   -lGL -lGLEW -l:libglfw.so.3 -lm   -o tutorial

后来,我看到这个也有效:

$ g++ tutorial.cpp   -lGL -lGLEW -lglfw -lm   -o tutorial

为什么会这样?

只是因为它是 libglfw.so 并且链接器搜索 libglfw3.so 因为您指定了 -lglfw3。请改用 -lglfw