user72708
user72708

Reputation: 1255

Linux - find the path of g++ lib

I installed these two libraries: glut and curl.

sudo apt-get install libcurl4-gnutls-dev
sudo apt-get install freeglut3-dev

when I have to compile a program I use:

g++ -o executable file11.cpp file2.cpp -lglut -lcurl

and it works!

How can I know where the linker search for "-lglut" or "-lcurl"?

"-lsomething" correspond to a path, How can I find out?

Upvotes: 0

Views: 6072

Answers (4)

Jonathan Wakely
Jonathan Wakely

Reputation: 171253

The linker will look in directories specified by the -L option and in standard system directories, which are usually /lib and /usr/lib. Although you're not using any -L options GCC will usually pass some to the linker so it can find GCC's own libraries (e.g. the C++ standard library) unless you use -nostdlib. GCC will also add -L options for the contents of the LIBRARY_PATH environment variable.

To see the options GCC passes to the linker you can compile with -v (for verbose) and to see which libraries the linker uses you can pass -Wl,--trace to GCC, which causes it to pass --trace to the linker, which makes it output something like:

/usr/bin/ld: mode elf_x86_64
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crt1.o
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crti.o
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/crtbegin.o
/tmp/ccJHrbSx.o
-lglut (/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libglut.so)
-lcurl (/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libcurl.so)
-lgcc_s (/usr/lib/gcc/x86_64-redhat-linux/4.7.2/libgcc_s.so)
/lib64/libc.so.6
(/usr/lib64/libc_nonshared.a)elf-init.oS
/lib64/ld-linux-x86-64.so.2
-lgcc_s (/usr/lib/gcc/x86_64-redhat-linux/4.7.2/libgcc_s.so)
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/crtend.o
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crtn.o

This shows the libraries that were found for the -lglut and lcurl libs on my system. The libs are found in that path because on my system GCC passed -L/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64 to the linker (shown by compiling with -v)

You can canonicalize those paths using readlink

$ readlink -f /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/
/usr/lib64/

Upvotes: 1

devnull
devnull

Reputation: 123448

Try

ldd /path/to/your/executable

Upvotes: 0

wheybags
wheybags

Reputation: 657

http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html It just turns the string into a filename, and checks normal locations for it, it seems.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798456

It checks /lib, /usr/lib, and any paths passed to -L.

Upvotes: 2

Related Questions