[动态库.so]
静态库.a, 被link到.so时
33 0000000000000610 <say_hello@plt>:
34 610: ff 25 12 0a 20 00 jmpq *0x200a12(%rip) # 201028 <_GLOBAL_OFFSET_TABLE_+0x28>
35 616: 68 02 00 00 00 pushq $0x2
36 61b: e9 c0 ff ff ff jmpq 5e0 <_init+0x28>
...
117 0000000000000715 <say_hello>:
118 715: 55 push %rbp
119 716: 48 89 e5 mov %rsp,%rbp
120 719: 48 83 ec 10 sub $0x10,%rsp
121 71d: 48 89 7d f8 mov %rdi,-0x8(%rbp)
122 721: 48 8d 3d 25 00 00 00 lea 0x25(%rip),%rdi # 74d <_fini+0x9>
123 728: e8 c3 fe ff ff callq 5f0 <puts@plt>
124 72d: c9 leaveq
125 72e: c3 retq
126
127 000000000000072f <_Z4funcv>:
128 72f: 55 push %rbp
129 730: 48 89 e5 mov %rsp,%rbp
130 733: 48 8d 3d 18 00 00 00 lea 0x18(%rip),%rdi # 752 <_fini+0xe>
131 73a: e8 d1 fe ff ff callq 610 <say_hello@plt>
132 73f: 5d pop %rbp
133 740: c3 retq
优先使用的是外部的say_hello.
[exec]
000000000040076d <main>:
40076d: 55 push %rbp
40076e: 48 89 e5 mov %rsp,%rbp
400771: e8 ea fe ff ff callq 400660 <_Z4funcv@plt>
400776: e8 c5 fe ff ff callq 400640 <_Z5func2v@plt>
40077b: bf 54 08 40 00 mov $0x400854,%edi
400780: e8 07 00 00 00 callq 40078c <say_hello>
400785: b8 00 00 00 00 mov $0x0,%eax
40078a: 5d pop %rbp
40078b: c3 retq
000000000040078c <say_hello>:
40078c: 55 push %rbp
40078d: 48 89 e5 mov %rsp,%rbp
400790: 48 83 ec 10 sub $0x10,%rsp
400794: 48 89 7d f8 mov %rdi,-0x8(%rbp)
400798: 8b 05 b6 08 20 00 mov 0x2008b6(%rip),%eax # 601054 <_ZZ9say_helloE1i>
40079e: 48 8b 55 f8 mov -0x8(%rbp),%rdx
4007a2: 48 89 d1 mov %rdx,%rcx
4007a5: 48 8d 15 e0 ff ff ff lea -0x20(%rip),%rdx # 40078c <say_hello>
4007ac: 89 c6 mov %eax,%esi
4007ae: 48 8d 3d a4 00 00 00 lea 0xa4(%rip),%rdi # 400859 <_IO_stdin_used+0x9>
4007b5: b8 00 00 00 00 mov $0x0,%eax
4007ba: e8 71 fe ff ff callq 400630 <printf@plt>
4007bf: 8b 05 8f 08 20 00 mov 0x20088f(%rip),%eax # 601054 <_ZZ9say_helloE1i>
4007c5: 83 c0 01 add $0x1,%eax
4007c8: 89 05 86 08 20 00 mov %eax,0x200886(%rip) # 601054 <_ZZ9say_helloE1i>
4007ce: c9 leaveq
4007cf: c3 retq
优先使用的是内部的say_hello.
[测试程序]
hello.h
#ifndef __HELLO__
#define __HELLO__
extern "C" {
void say_hello(char* name);
}
#endif
hello.c
#include <stdio.h>
extern "C" {
void say_hello(char* name)
{
static int i = 0;
printf("[%d][%p]Hello %s!\n", i, say_hello, name);
i++;
}
}
libhello.c
#include <stdio.h>
extern "C" {
void say_hello(char*)
{
printf("1111\n");
}
}
void func()
{
say_hello("dgd");
}
libhello2.c
#include <stdio.h>
#include "hello.h"
void func2()
{
say_hello("dgd");
}
main.c
#include <stdio.h>
#include "hello.h"
#include "testhello.h"
#include "testhello2.h"
int main()
{
func();
func2();
//say_hello(); // can be uncomment to test
return 0;
}
[compile & link]
g++ -shared -fPIC -c hello.c
ar -crv libhello.a hello.o
g++ -shared -fPIC libhello.c -o libTestHello.so
g++ -shared -fPIC libhello2.c -o libTestHello2.so
g++ main.c -Wl,--whole-archive libhello.a -Wl,--no-whole-archive -L. -lTestHello -lTestHello2 -Wl,-rpath=. -o Test
or
g++ main.c -L. -lTestHello -lTestHello2 -Wl,-rpath=. -o Test
ref:
https://www.cnblogs.com/nobugtodebug/archive/2012/11/07/e6cd72c67b3dd843f40d7ce919f7336a.html
https://www.cnblogs.com/JimmyTY/p/5856217.html
https://www.cnblogs.com/coderzh/p/LinkAllSymbols.html