简体   繁体   English

静态与动态链接

[英]Static vs Dynamic Linking

I'm trying to understand how the ELF looks like for a statically vs. a dynamically linked program. 我试图理解ELF对静态与动态链接程序的看法。

I understand that this is how static linking works: 我知道这是静态链接的工作原理: 在此输入图像描述

In my case, I have two files, foo.c and bar.c . 在我的例子中,我有两个文件, foo.cbar.c I also have their object files; 我也有他们的目标文件; foo.o and bar.o . foo.obar.o With the objdump command, I can see the relocations in each file. 使用objdump命令,我可以在每个文件中看到重定位。

How do I statically link the foo.o and bar.o ? 如何静态链接foo.obar.o

How do I dynamically link the foo.o and bar.o ? 如何动态链接foo.obar.o

How can I see the difference in the output files? 如何查看输出文件的区别?

Linking dynamically is the default mode of most linkers these days. 动态链接是目前大多数链接器的默认模式。 If you want to link statically you have to use the -static flag when linking. 如果要静态链接,则必须在链接时使用-static标志。 To clarify, when I say "linking dynamically" versus "linking statically" I mean the linking with external libraries, and not generating a library that in turn can be linked (dynamically or statically). 为了澄清,当我说“动态链接”与“静态链接”时,我指的是与外部库的链接,而不是生成一个库,而这个库又可以链接(动态或静态)。

The difference can't be seen in the object files you pass to the linker, as it has nothing to do with the compiler and object-file generation, the result can only be seen in the resulting executable program after linking, and the biggest difference is that the executable will most likely be larger. 在传递给链接器的目标文件中无法看到差异,因为它与编译器和对象文件生成无关,结果只能在链接后生成的可执行程序中看到,并且最大的区别是可执行文件很可能会更大。

The resulting and fully linked executable will be larger because then all the libraries (for which there are static libraries) will actually be linked into the executable program quite literally. 生成的和完全链接的可执行文件将更大,因为所有库(其中都有静态库)实际上将完全链接到可执行程序。 It's basically including the libraries object files together with your own object files. 它基本上包括库对象文件和您自己的目标文件。 Actually, on POSIX platforms static libraries are just archives of object files. 实际上,在POSIX平台上,静态库只是目标文件的归档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM