usr/bin/ld: cannot find -l


I'm trying to compile my program and it returns this error :

usr/bin/ld: cannot find -l<nameOfTheLibrary>

in my makefile I use the command g++ and link to my library which is a symbolic link to my library located on an other directory.

Is there an option to add to make it work please?

2/27/2016 12:44:40 AM

Accepted Answer

If your library name is say and it is located on path say:


then to link it to your program:

g++ -L/home/user/myDir -lxyz myprog.cpp -o myprog
1/10/2017 3:59:36 PM

There does not seem to be any answer which addresses the very common beginner problem of failing to install the required library in the first place.

On Debianish platforms, if libfoo is missing, you can frequently install it with something like

apt-get install libfoo-dev

The -dev version of the package is required for development work, even trivial development work such as compiling source code to link to the library.

The package name will sometimes require some decorations (libfoo0-dev? foo-dev without the lib prefix? etc), or you can simply use your distro's package search to find out precisely which packages provide a particular file.

(If there is more than one, you will need to find out what their differences are. Picking the coolest or the most popular is a common shortcut, but not an acceptable procedure for any serious development work.)

For other architectures (most notably RPM) similar procedures apply, though the details will be different.


Compile Time

When g++ says cannot find -l<nameOfTheLibrary>, it means that g++ looked for the file lib{nameOfTheLibrary}.so, but it couldn't find it in the shared library search path, which by default points to /usr/lib and /usr/local/lib and somewhere else maybe.

To resolve this problem, you should either provide the library file (lib{nameOfTheLibrary}.so) in those search paths or use -L command option. -L{path} tells the g++ (actually ld) to find library files in path {path} in addition to default paths.

Example: Assuming you have a library at /home/taylor/, and you want to link your app to this library. In this case you should supply the g++ with the following options:

g++ main.cpp -o main -L/home/taylor -lswift
  • Note 1: -l option gets the library name without lib and .so at its beginning and end.

  • Note 2: In some cases, the library file name is followed by its version, for instance In these cases, g++ also cannot find the library file. A simple workaround to fix this is creating a symbolic link to called


When you link your app to a shared library, it's required that library stays available whenever you run the app. In runtime your app (actually dynamic linker) looks for its libraries in LD_LIBRARY_PATH. It's an environment variable which stores a list of paths.

Example: In case of our example, dynamic linker cannot find in LD_LIBRARY_PATH (which points to default search paths). To fix the problem you should append that variable with the path is in.

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/taylor

During compilation with g++ via make define LIBRARY_PATH if it may not be appropriate to change the Makefile with the -Loption. I had put my extra library in /opt/lib so I did:

$ export LIBRARY_PATH=/opt/lib/

and then ran make for successful compilation and linking.

To run the program with a shared library define:

$ export LD_LIBRARY_PATH=/opt/lib/

before executing the program.


First, you need to know the naming rule of lxxx:

/usr/bin/ld: cannot find -lc
/usr/bin/ld: cannot find -lltdl
/usr/bin/ld: cannot find -lXtst

lc means, lltdl means, lXtst means

So, it is lib + lib-name + .so

Once we know the name, we can use locate to find the path of this file.

$ locate
/home/user/anaconda3/lib/   # <-- right here

If you cannot find it, you need to install it by yum (I use CentOS). Usually you have this file, but it does not link to right place.

Link it to the right place, usually it is /lib64 or /usr/lib64

$ sudo ln -s /home/user/anaconda3/lib/ /usr/lib64/




When you compile your program you must supply the path to the library; in g++ use the -L option:

g++ -o myprogram -lmylib -L/path/foo/bar