I\'m trying to compile my program and it returns this error :
usr/bin/ld: cannot find -l
in my makefile I use the c
When g++ says cannot find -l
, 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/libswift.so
, 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 libswift.so.1.2
. In these cases, g++ also cannot find the library file. A simple workaround to fix this is creating a symbolic link to libswift.so.1.2
called libswift.so
.
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 libswift.so
example, dynamic linker cannot find libswift.so
in LD_LIBRARY_PATH
(which points to default search paths). To fix the problem you should append that variable with the path libswift.so
is in.
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/taylor