ldd command in linux
https://blog.csdn.net/nzjdsds/article/details/86759843
In linux, some commands are common to everyone, such as ls, rm, mv, cp, etc. I don't think it's necessary to go into details. And some commands are only used by developers. As programmers, we need to know about these commands, and some even need to be proficient in using them.
Some people always say that these orders are unimportant. It's OK to check them when using them. What a nonsense. The specific usage details can be checked, but at least you need to know that there is ldd. Even ldd does not know, how to know what is ldd?
In this article, let's introduce the ldd command, although it's very simple. Oh, I suddenly remember that I have a friend whose name is three words, and the initial is just l, d, d, which is a little interesting. In linux, ldd is the abbreviation of list, dynamic, dependencies, which means to list dynamic library dependencies. Of course, you can also use ldd --help or man ldd to see its usage. Let's also look at:
The content of test.h is:
void print();
The content of test.c is:
-
#include <stdio.h> #include "test.h" void print() { printf("rainy days\n");
}
The content of main.c is:
-
#include "test.h" int main() { print(); return 0; }
Carry out a series of compilation, and use ldd command to get:
[taoge@localhost learn_ldd]$ ls
main.c test.c test.h
[taoge@localhost learn_ldd]$ gcc -c main.c test.c
[taoge@localhost learn_ldd]$ gcc main.o test.o
[taoge@localhost learn_ldd]$ ls
a.out main.c main.o test.c test.h test.o
[taoge@localhost learn_ldd]$ ./a.out
rainy days
[taoge@localhost learn_ldd]$
[taoge@localhost learn_ldd]$
[taoge@localhost learn_ldd]$
[taoge@localhost learn_ldd]$ ldd *
a.out:
linux-gate.so.1 => (0x00ba1000)
libc.so.6 => /lib/libc.so.6 (0x0087e000)
/lib/ld-linux.so.2 (0x00858000)
main.c:
ldd: warning: you do not have execution permission for `./main.c'
not a dynamic executable
main.o:
ldd: warning: you do not have execution permission for `./main.o'
not a dynamic executable
test.c:
ldd: warning: you do not have execution permission for `./test.c'
not a dynamic executable
test.h:
ldd: warning: you do not have execution permission for `./test.h'
lddlibc4: cannot read header from `./test.h'
test.o:
ldd: warning: you do not have execution permission for `./test.o'
not a dynamic executable
[taoge@localhost learn_ldd]$
Can see a.out Depend on libc.so.6 This library, and the path of this library is/lib/libc.so.6
Let's continue to look at the use of static link libraries:
[taoge@localhost learn_ldd]$ ls
main.c test.c test.h
[taoge@localhost learn_ldd]$ gcc -c test.c
[taoge@localhost learn_ldd]$ ar rcs libtest.a test.o
[taoge@localhost learn_ldd]$ gcc main.c -L. -ltest
[taoge@localhost learn_ldd]$ ls
a.out libtest.a main.c test.c test.h test.o
[taoge@localhost learn_ldd]$ ./a.out
rainy days
[taoge@localhost learn_ldd]$
[taoge@localhost learn_ldd]$
[taoge@localhost learn_ldd]$
[taoge@localhost learn_ldd]$ ldd *
a.out:
linux-gate.so.1 => (0x00e7c000)
libc.so.6 => /lib/libc.so.6 (0x0087e000)
/lib/ld-linux.so.2 (0x00858000)
libtest.a:
ldd: warning: you do not have execution permission for `./libtest.a'
not a dynamic executable
main.c:
ldd: warning: you do not have execution permission for `./main.c'
not a dynamic executable
test.c:
ldd: warning: you do not have execution permission for `./test.c'
not a dynamic executable
test.h:
ldd: warning: you do not have execution permission for `./test.h'
lddlibc4: cannot read header from `./test.h'
test.o:
ldd: warning: you do not have execution permission for `./test.o'
not a dynamic executable
[taoge@localhost learn_ldd]$
This time, the static library is used, and the result is almost the same. There is nothing to say.
Let's continue to look at the use of dynamic link libraries:[taoge@localhost learn_ldd]$ ls
main.c test.c test.h
[taoge@localhost learn_ldd]$ gcc -c test.c
[taoge@localhost learn_ldd]$ gcc -shared -fPIC -o libtest.so test.o
[taoge@localhost learn_ldd]$ gcc main.c -L. -ltest
[taoge@localhost learn_ldd]$ ls
a.out libtest.so main.c test.c test.h test.o
[taoge@localhost learn_ldd]$ ./a.out
./a.out: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory
[taoge@localhost learn_ldd]$
[taoge@localhost learn_ldd]$
[taoge@localhost learn_ldd]$
[taoge@localhost learn_ldd]$ ldd *
a.out:
linux-gate.so.1 => (0x00f3d000)
libtest.so => not found
libc.so.6 => /lib/libc.so.6 (0x0087e000)
/lib/ld-linux.so.2 (0x00858000)
libtest.so:
linux-gate.so.1 => (0x0031d000)
libc.so.6 => /lib/libc.so.6 (0x00110000)
/lib/ld-linux.so.2 (0x00858000)
main.c:
ldd: warning: you do not have execution permission for `./main.c'
not a dynamic executable
test.c:
ldd: warning: you do not have execution permission for `./test.c'
not a dynamic executable
test.h:
ldd: warning: you do not have execution permission for `./test.h'
lddlibc4: cannot read header from `./test.h'
test.o:
ldd: warning: you do not have execution permission for `./test.o'
not a dynamic executable
[taoge@localhost learn_ldd]$
[taoge@localhost learn_ldd]$ su root
Password:
[root@localhost learn_ldd]# cp libtest.so /usr/lib/
[root@localhost learn_ldd]# ./a.out
rainy days
[root@localhost learn_ldd]# exit
exit
[taoge@localhost learn_ldd]$ ./a.out
rainy days
[taoge@localhost learn_ldd]$
[taoge@localhost learn_ldd]$
[taoge@localhost learn_ldd]$
[taoge@localhost learn_ldd]$ ldd a.out
linux-gate.so.1 => (0x00510000)
libtest.so => /usr/libtest.so (0x00fe3000)
libc.so.6 => /lib/libc.so.6 (0x0087e000)
/lib/ld-linux.so.2 (0x00858000)
First of all, we can see that a.out depends on libtest.so, but the result is not found. Why? Because there is no libtest.so under / usr/lib, later, I copied libtest.so (root permission required), and it was OK. In addition, we should also see that the dependency Library of libtest.so can also be found through the ldd command.
Of course, if you don't want to write your own program, but you want to try the ldd command, you can do the following:
[taoge@localhost learn_ldd]$ ldd /bin/ls
linux-gate.so.1 => (0x0052b000)
libselinux.so.1 => /lib/libselinux.so.1 (0x00b52000)
librt.so.1 => /lib/librt.so.1 (0x00a5c000)
libcap.so.2 => /lib/libcap.so.2 (0x0489c000)
libacl.so.1 => /lib/libacl.so.1 (0x048c9000)
libc.so.6 => /lib/libc.so.6 (0x0087e000)
libdl.so.2 => /lib/libdl.so.2 (0x00a0c000)
/lib/ld-linux.so.2 (0x00858000)
libpthread.so.0 => /lib/libpthread.so.0 (0x00a13000)
libattr.so.1 => /lib/libattr.so.1 (0x04d99000)
[taoge@localhost learn_ldd]$ ldd /bin/mv
linux-gate.so.1 => (0x00944000)
libselinux.so.1 => /lib/libselinux.so.1 (0x00b52000)
librt.so.1 => /lib/librt.so.1 (0x00a5c000)
libacl.so.1 => /lib/libacl.so.1 (0x048c9000)
libattr.so.1 => /lib/libattr.so.1 (0x04d99000)
libc.so.6 => /lib/libc.so.6 (0x00110000)
libdl.so.2 => /lib/libdl.so.2 (0x00a0c000)
/lib/ld-linux.so.2 (0x00858000)
libpthread.so.0 => /lib/libpthread.so.0 (0x00a13000)
[taoge@localhost learn_ldd]$
In the actual linux development and debugging, we should often check the dynamic library dependency, ldd is still used more, especially in case of failure. OK, the ldd command is simply introduced here. Although it is simple, it is very practical, so it is not unknown.