Why link libraries (like pthread) when they are in the right folder "/lib" and "/usr/lib"?
Asked Answered
M

1

5

1. Why do we need to link the non standard libraries/include non standard header files when they are already present in the right folder

anirudh@anirudh-Aspire-5920:~/Documents/DUMP$ locate libpthread
/lib/libpthread-2.12.1.so
/lib/libpthread.so.0
/usr/lib/libpthread.a
/usr/lib/libpthread.so
/usr/lib/libpthread_nonshared.a
/usr/lib/xen/libpthread.a
/usr/lib/xen/libpthread_nonshared.a
anirudh@anirudh-Aspire-5920:

The man page of ld.so/ld-linux.so - dynamic linker/loader says that the necessary libraries required by a program are searched In the default path /lib, and then /usr/lib. When my library's .so file is already there in /lib folder then why do I need to link it exclusively. Also the -l option is used to link static libraries. but when I do pmap of the process I see that the dynamic library of pthread with .so extension is being used rather than the one with .a extension. Similarly

anirudh@anirudh-Aspire-5920:~/Documents/DUMP$ locate mysql.h
/usr/include/mysql/mysql.h
anirudh@anirudh-Aspire-5920:~/Documents/DUMP$

When it is already present in the folder /usr/include which is the standard folder for all header files then why do I need to include it exclusively using -I option.

Mutualize answered 6/3, 2011 at 7:15 Comment(0)
E
10
  1. Although the linker searches in /lib and /usr/lib for libraries requested, this does not mean that it automatically loads all of those libraries. Loading a library is a fairly expensive operation, so the linker only loads libraries it knows will be needed. -l is what tells it the library is needed. There are some OSes and toolchains which automatically try to figure out what libraries are needed based on directives in the headers (Visual C++ does this on windows), but this technique is not used on Linux.
  2. -l is used for both static and shared libraries. If both are present, the shared version will be used, unless -static is specified to the linker.
  3. If you #include <mysql/mysql.h>, the preprocessor will look in /usr/include/mysql/mysql.h for it. That is, the search is not recursive - if you specify <mysql.h> the preprocessor will look at /usr/include/mysql.h but not /usr/include/mysql/mysql.h.
Einkorn answered 6/3, 2011 at 7:35 Comment(4)
Thanks for the answer. I recently wrote a code which used math library functions. I compiled it without linking library (the manual pages says we have to give -lm ) and it worked. A few days ago it had given me the error that function "sqrt()" not found...so i had to link. So why is it not giving the same error today ?Mutualize
@Anirudh, compilers will sometimes replace some of the basic math functions with inline variants. Whether this happens depends on many factors, including the compile flags and exactly how the function is being used. If it's not replaced with an inline variant, then -lm will become necessary - so it's best to just pass -lm in case.Einkorn
thanks a tonne for solving my query. your answers are to the point and very helpful.Mutualize
i suppose you mean the preprocessor will look in /usr/include ... not the linker, which don't even know what a header is.Nausea

© 2022 - 2024 — McMap. All rights reserved.