Search code examples
c++g++glew

Program compiles, but cannot run because of missing library that exists


I have an OpenGL program. I have all the include directories, and everything.

Directory Structure:

Main.cpp
Lib/
GL/
GLEW/
glm/

I compile the program by running:

g++ main.cpp -lGL -lm -lX11 -lpthread -lXi -lXrandr -ldl -I. -lglfw -Llib/ -o main -lGLEW

The error is on -lGLEW. The program compiles with no errors, but when I run ./main, it gives me this:

Error while loading shared libraries: libGLEW.so.2.2: No such file or directory.

This is confusing, as in my lib/ directory, libGLEW.so.2.2 is present, so is libGLEW.so and libGLEW.a.

Can someone please help me?


Solution

  • When running a dynamic linked executable, the linker must be able to find all the libraries it needs. It always searches

    • a list of fixed default paths like /lib and /usr/lib
    • additional paths defined by the environment variable LD_LIBRARY_PATH
    • any non-standard paths hard-coded in the binary by the -Wl,-rpath g++ option.

    These are your options for making this non-standard library known to the runtime linker.

    So either use export LD_LIBRARY_PATH=<somepath>/lib when running or use -Wl,-rpath=<somepath>/lib (with comma and without spaces) when building.