1.当非标准库已经存在于正确的文件夹中时,为什么我们需要链接非标准库/包含非标准头文件
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:
ld.so/ld-linux.so - 动态链接器/加载器的手册页
表示在默认路径 /lib 中搜索程序所需的必要库,然后在 /usr/lib 中搜索。
当我的库的 .so 文件已经存在于 /lib 文件夹中时,为什么我需要专门链接它。
-l
选项还用于链接静态库。但是当我对进程进行 pmap 时,我发现正在使用具有 .so
扩展名的 pthread 动态库,而不是具有 .a
扩展名的 pthread 动态库。
类似地,
anirudh@anirudh-Aspire-5920:~/Documents/DUMP$ locate mysql.h
/usr/include/mysql/mysql.h
anirudh@anirudh-Aspire-5920:~/Documents/DUMP$
当它已经存在于文件夹 /usr/include
中(这是所有头文件的标准文件夹)时,为什么我需要使用 -I
选项专门包含它。
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.
发布评论
评论(1)
/lib
和/usr/lib
中搜索请求的库,但这并不意味着它会自动加载所有这些库。加载库是一项相当昂贵的操作,因此链接器仅加载它知道需要的库。-l
告诉它需要该库。有一些操作系统和工具链会根据标头中的指令自动尝试找出需要哪些库(Visual C++ 在 Windows 上执行此操作),但 Linux 上不使用此技术。-l
用于静态库和共享库。如果两者都存在,则将使用共享版本,除非为链接器指定了-static
。/usr/include/mysql/mysql.h
中查找它。也就是说,搜索不是递归的 - 如果您指定/usr/include/mysql.h
,但不会/usr/include/mysql/mysql.h
。/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.-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.#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
.