动态 link 尝试全部静态生成 linked 二进制文件?为什么?
dynamic link attempts all yield statically linked binaries? Why?
我正在按照 "T" here 关于在 Linux 中创建动态 linked 共享库的教程进行操作,当我按照说明进行操作时, gcc 似乎静态 link 库。
本教程推荐了 3 个文件:foo.c
、foo.h
和 main.c
。 Main 包括 foo.h
和调用 foo()
,在 foo.c
.
中定义
我对调试教程做了一点改动...我的 foo
看起来像这样:
void foo(void) {
int i = 54321;
printf( "Shared lib: %d\n", i );
}
它告诉我使用这 3 个步骤进行编译:
gcc -c -Wall -Werror -fpic foo.c
gcc -shared -o libfoo.so foo.o
gcc -L/home/username/foo -Wall -o test main.c -lfoo
当我 运行 ./test
时,它起作用了,我可以从 foo()
看到 "hello 54321"。事实上,它工作得很好,如果我删除 libfoo.so
,它就会工作。看起来很可疑,所以我做了 objdump -S test
并在目标文件中找到了这个小家伙:
000000000000068a <foo>:
68a: 55 push %rbp
68b: 48 89 e5 mov %rsp,%rbp
68e: 48 83 ec 10 sub [=13=]x10,%rsp
692: c7 45 fc 31 d4 00 00 movl [=13=]xd431,-0x4(%rbp)
^^^ there's my constant, 54321, in hex.
should be in the "dynamic" object, not here, right?
699: 8b 45 fc mov -0x4(%rbp),%eax
69c: 89 c6 mov %eax,%esi
69e: 48 8d 3d af 00 00 00 lea 0xaf(%rip),%rdi # 754 <_IO_stdin_used+0x4>
6a5: b8 00 00 00 00 mov [=13=]x0,%eax
6aa: e8 b1 fe ff ff callq 560 <printf@plt>
6af: 90 nop
6b0: c9 leaveq
6b1: c3 retq
我做错了什么?
提前谢谢你...
P.S。使用 version gcc (Ubuntu 7.2.0-8ubuntu3.2) 7.2.0
在 x86_64 Debian Stretch 上编译
What am I doing wrong?
您很可能在 /home/username/foo
目录中有 libfoo.a
。
或者您不小心使用了 #include "foo.c"
,而本应使用 #include "foo.h"
。
您可以尝试找出 foo()
的定义进入 test
的位置:
gcc -L/home/username/foo -Wall -o test main.c -lfoo -Wl,-y,foo
应该显示 reference to foo
来自一些 /tmp/xyz.o
和 definition 来自 <somewhere>
.
我正在按照 "T" here 关于在 Linux 中创建动态 linked 共享库的教程进行操作,当我按照说明进行操作时, gcc 似乎静态 link 库。
本教程推荐了 3 个文件:foo.c
、foo.h
和 main.c
。 Main 包括 foo.h
和调用 foo()
,在 foo.c
.
我对调试教程做了一点改动...我的 foo
看起来像这样:
void foo(void) {
int i = 54321;
printf( "Shared lib: %d\n", i );
}
它告诉我使用这 3 个步骤进行编译:
gcc -c -Wall -Werror -fpic foo.c
gcc -shared -o libfoo.so foo.o
gcc -L/home/username/foo -Wall -o test main.c -lfoo
当我 运行 ./test
时,它起作用了,我可以从 foo()
看到 "hello 54321"。事实上,它工作得很好,如果我删除 libfoo.so
,它就会工作。看起来很可疑,所以我做了 objdump -S test
并在目标文件中找到了这个小家伙:
000000000000068a <foo>:
68a: 55 push %rbp
68b: 48 89 e5 mov %rsp,%rbp
68e: 48 83 ec 10 sub [=13=]x10,%rsp
692: c7 45 fc 31 d4 00 00 movl [=13=]xd431,-0x4(%rbp)
^^^ there's my constant, 54321, in hex.
should be in the "dynamic" object, not here, right?
699: 8b 45 fc mov -0x4(%rbp),%eax
69c: 89 c6 mov %eax,%esi
69e: 48 8d 3d af 00 00 00 lea 0xaf(%rip),%rdi # 754 <_IO_stdin_used+0x4>
6a5: b8 00 00 00 00 mov [=13=]x0,%eax
6aa: e8 b1 fe ff ff callq 560 <printf@plt>
6af: 90 nop
6b0: c9 leaveq
6b1: c3 retq
我做错了什么? 提前谢谢你...
P.S。使用 version gcc (Ubuntu 7.2.0-8ubuntu3.2) 7.2.0
What am I doing wrong?
您很可能在 /home/username/foo
目录中有 libfoo.a
。
或者您不小心使用了 #include "foo.c"
,而本应使用 #include "foo.h"
。
您可以尝试找出 foo()
的定义进入 test
的位置:
gcc -L/home/username/foo -Wall -o test main.c -lfoo -Wl,-y,foo
应该显示 reference to foo
来自一些 /tmp/xyz.o
和 definition 来自 <somewhere>
.