Error while loading shared library: libglfw.so.3?

Hi
I’m learning opengl and so i’m trying to get a project setup with glfw and glew on linux but im having some issues.

This is how i went about installing glfw: I first cloned the glfw repository from github and then installed the dependencies using the following command: sudo apt install xorg-dev

I then went in to the glfw folder and typed: cmake -DBUILD_SHARED_LIBS=ON .
This generated a makefile. I then typed: make all & make install.

I then wrote a small c++ program that just consist of main and a call to glfwInit(), nothing else.
When i compile the program using the following command: g++ mainer.cpp -Wall -lglfw
I get no errors, but when i try to run it i get the following: ./a.out: error while loading shared libraries: libglfw.so.3: cannot open shared object file: No such file or directory

Oddly enough, when i remove the glfwInit() call, it works fine?

Does anybody know what is going on and what i’m doing wrong for this to happen?

Thank in advance!

On Linux .so shared libraries are not loaded from current folder like .dll files on Windows.
They are loaded only from specific locations on your system, like /usr/lib or similar.

To override location where to load .so files, you can use LD_LIBRARY_PATH environment variable. Just set it to location where libglfw.so.3 file is, like this:

LD_LIBRARY_PATH=/path/to/directory ./your_executable

Alternative approach is to modify binary you are building to make it load shared library from current folder. You can do that with $ORIGIN feature of dynamic library loader: http://man7.org/linux/man-pages/man8/ld.so.8.html
Basically you need to pass extra -Wl,-rpath,'$ORIGIN' argument to gcc. Make sure it does not expand $ORIGIN, it must have $ sign there. This will make ld to search for libraries in same folder as executable.

2 Likes