包含和/或链接二进制文件未使用的内容会产生什么负面影响?

[英]What are the negative consequences of including and/or linking things that aren't used by your binary?


Let's say that I have a binary that I am building, and I include a bunch of files that are never actually used, and do the subsequent linking to the libraries described by those include files? (again, these libraries are never used)

假设我有一个我正在构建的二进制文件,并且我包含了一堆从未实际使用过的文件,并且后续链接到这些包含文件所描述的库? (再次,这些库从未使用过)

What are the negative consequences of this, beyond increased compile time?

除了增加编译时间之外,这有什么负面影响?

13 个解决方案

#1


8  

A few I can think of are namespace pollution and binary size

我能想到的一些是命名空间污染和二进制大小

#2


5  

In addition to compile time; Increased complexity, needless distraction while debugging, a maintenance overhead.

除了编译时间;增加了复杂性,在调试时不必要的分心,维护开销。

Apart from that, nothing.

除此之外,什么都没有。

#3


4  

In addition to what Sasha lists is maintenance cost. Will you be able to detect easily what is used and what is not used in the future when and if you chose to remove unused stuff?

除了Sasha列出的维护成本。如果您选择删除未使用的东西,您是否能够轻松检测到将来使用的和未使用的内容?

#4


4  

If the libraries are never used, there should be no increase in size for the executable.

如果从未使用过库,则可执行文件的大小不应增加。

#5


3  

Depending on the exact linker, you might also notice that the global objects of your unused libraries still get constructed. This implies a memory overhead and increases startup costs.

根据确切的链接器,您可能还会注意到仍未构建未使用的库的全局对象。这意味着内存开销并增加启动成本。

#6


1  

If the libraries you're including but not using aren't on the target system, it won't be able to compile even though they aren't necessary.

如果您包含但未使用的库不在目标系统上,则即使它们不是必需的,也无法编译。

#7


1  

Here is my answer for a similar question concerning C and static libraries. Perhaps it is useful to you in the context of C++ as well.

以下是关于C和静态库的类似问题的答案。也许它在C ++环境中对你有用。

#8


1  

You mention an increase in the compilation time. From that I understand the libraries are statically linked, and not dynamically. In this case, it depends how the linker handles unused functions. If it ignores them, you will have mostly maintenance problems. If they’ll be included, the executable size will increase. Now, this is more significant than the place it takes on the hard drive. Large executables could run slower due to caching issues. If active code and non-active code are adjacent in the exe, they will be cached together, making the cache effectively smaller and less efficient.

你提到了编译时间的增加。据我所知,这些库是静态链接的,而不是动态的。在这种情况下,它取决于链接器如何处理未使用的函数。如果忽略它们,您将主要遇到维护问题。如果它们将被包含,则可执行文件大小将增加。现在,这比它在硬盘驱动器上的位置更重要。由于缓存问题,大型可执行文件可能会运行得更慢。如果活动代码和非活动代码在exe中相邻,它们将被缓存在一起,使缓存有效地变小并且效率降低。

VC2005 and above have an optimization called PGO, which orders the code within the executable in a way that ensures effective caching of code that is often used. I don’t know if g++ has a similar optimization, but it’s worth looking into that.

VC2005及更高版本有一个名为PGO的优化,它以确保有效缓存经常使用的代码的方式对可执行文件中的代码进行排序。我不知道g ++是否有类似的优化,但值得研究。

#9


1  

A little compilation here of the issues, wiki-edit it as necessary:

这里有一些关于问题的汇编,wiki-根据需要编辑它:

The main problem appears to be: Namespace Pollution This can cause problems in future debugging, version control, and increased future maintenance cost.

主要问题似乎是:命名空间污染这可能会导致将来的调试,版本控制和未来维护成本增加的问题。

There will also be, at the minimum, minor Binary Bloat, as the function/class/namespace references will be maintained (In the symbol table?). Dynamic libraries should not greatly increase binary size(but they become a dependency for the binary to run?). Judging from the GNU C compiler, statically linked libraries should not be included in final binary if they are never referenced in the source. (Assumption based on the C compiler, may need to clarify/correct)

由于函数/类/命名空间引用将被维护(在符号表中?),因此至少还会有次要的二进制膨胀。动态库不应该大大增加二进制大小(但它们成为二进制运行的依赖项?)。从GNU C编译器来看,如果从未在源代码中引用静态链接库,则不应将其包含在最终二进制文件中。 (基于C编译器的假设,可能需要澄清/纠正)

Also, depending on the nature of your libraries, global and static objects/variables may be instantiated, causing increased startup time and memory overhead.

此外,根据库的性质,可能会实例化全局和静态对象/变量,从而导致启动时间和内存开销增加。

Oh, and increased compile/linking time.

哦,增加了编译/链接时间。

#10


1  

I find it frustrating when I edit a file in the source tree because some symbol that I'm working on appears in the source file (e.g. a function name, where I've just changed the prototype - or, sadly but more typically, just added the prototype to a header) so I need to check that the use is correct, or the compiler now tells me the use in that file is incorrect. So, I edit the file. Then I see a problem - what is this file doing? And it turns out that although the code is 'used' in the product, it really isn't actively used at all.

当我在源代码树中编辑文件时,我发现它很令人沮丧,因为我正在处理的某个符号出现在源文件中(例如,函数名称,我刚刚更改了原型 - 或者,遗憾的是,更典型的是,只是将原型添加到标题中)所以我需要检查使用是否正确,或者编译器现在告诉我该文件中的使用是不正确的。所以,我编辑文件。然后我看到一个问题 - 这个文件在做什么?事实证明,虽然代码在产品中被“使用”,但它实际上并没有被主动使用。

I found an occurrence of this problem on Monday. A file with 10,000+ lines of code invoked a function 'extern void add_remainder(void);' with an argument of 0. So, I went to fix it. Then I looked at the rest of the code...it turned out it was a development stub from about 15 years ago that had never been removed. Cleanly excising the code turned out to involve minor edits to more than half-a-dozen files - and I've not yet worked out whether it is safe to remove the enumeration constant from the middle of an enumeration in case. Temporarily, that is marked 'Unused/Obsolete - can it be removed safely?'.

我星期一发现了这个问题。具有10,000多行代码的文件调用函数'extern void add_remainder(void);'参数为0.所以,我去解决它。然后我查看了剩下的代码......结果发现它是大约15年前的一个开发存根,从未删除过。干净地删除代码结果是对超过六个文件进行了少量编辑 - 我还没有确定从枚举中间删除枚举常量是否安全。暂时标记为“未使用/已过时 - 是否可以安全删除?”。

That chunk of code has had zero cove coverage for the last 15 years - production, test, ... True, it's only a tiny part of a vast system - percentage-wise, it's less than a 1% blip on the chart. Still, it is extra wasted code.

在过去的15年中,这一大块代码的覆盖范围为零 - 生产,测试......真的,它只是庞大系统中的一小部分 - 百分比方面,它在图表上的不到1%。不过,这是额外浪费的代码。

Puzzling. Annoying. Depressingly common (I've logged, and fixed, at least half a dozen similar bugs this year so far).

令人费解。烦人。令人沮丧的是(到目前为止,我已经记录并修复了至少6个类似的错误)。

And a waste of my time - and other developers' time. The file had been edited periodically over the years by other people doing what I was doing - a thorough job.

浪费我的时间 - 和其他开发人员的时间。多年来,其他人正在做我正在做的事情 - 这是一份彻底的工作。

#11


0  

I have never experienced any problems with linking a .lib file of which only a very small part is used. Only the code that is really used will be linked into the executable, and the linking time did not increase noticeably (with Visual Studio).

我从来没有遇到任何链接.lib文件的问题,该文件只使用了很小的部分。只有真正使用的代码才会链接到可执行文件中,并且链接时间没有明显增加(使用Visual Studio)。

#12


0  

If you link to binaries and they get loaded at runtime, they may perform non-trivial initialization which can do anything from allocate a small amount of memory to consume scarce resources to alter the state of your module in ways you don't expect, and beyond.

如果链接到二进制文件并且它们在运行时被加载,它们可以执行非平凡的初始化,这可以做任何事情,从分配少量内存到消耗稀缺资源,以您不期望的方式改变模块的状态,以及超越。

You're better off getting rid of stuff you don't need, simply to eliminate a bunch of unknowns.

你最好摆脱你不需要的东西,只是为了消除一堆未知数。

#13


0  

It could perhaps even fail to compile if the build tree isn't well maintained. if your'e compiling on embedded systems without swap space. The compiler can run out of memory while trying to compile a massive object file.

如果构建树没有得到很好的维护,它甚至可能无法编译。如果您在没有交换空间的情况下编译嵌入式系统。在尝试编译大量目标文件时,编译器可能会耗尽内存。

It happened at work to us recently.

它最近发生在我们的工作中。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/04/15/c07d7e8fe4bf93cda6b4bef1a3e2ab4d.html



 
  © 2014-2022 ITdaan.com 联系我们: