When I compile a simple C program for Linux, how does it automatically know where to find the standard C libraries when I run it?
Take the simple Hello World program for example which uses printf to print "Hello World". I can simply compile this with gcc without specifying the paths to libraries and hence to where the printf() function is, and it works.
My guess is Linux will automatically look in the places defined by the PATH variable such as :
/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
My other guess is when my program is compiled, gcc implicitly dynamically links my program to these libraries and hence can be found when run.
ld(1)
) find the libraries needed by the program? ~ in the paths given via the-L
option; compile withgcc -v
to see them b) where does the dynamic linker find the shared libraries needed by your prog ~ in paths burned in into the executable, specified via envvars, configured by ldconfig(1) and also in some fixed paths ;-) Please read theld.so(8)
manpage for all the details, and try to narrow down your Q. – Jun 07 '19 at 00:54