Search code examples
c++linuxg++ldglfw

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


I was trying to build a file using:

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

And was getting:

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

I checked the package and the file were already there:

$ 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

I saw instructions on this question (about other library) to make a link of the files:

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

I looked up and my system did have it:

$ 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 a saw on another answer:

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 [...]

I tried, and this did work:

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

Later on, I saw this also worked:

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

Why does this happens?


Solution

  • Simply because it’s libglfw.so and the linker searches for libglfw3.so because you specified -lglfw3. Use -lglfw instead.