包含和/或鏈接二進制文件未使用的內容會產生什么負面影響?

[英]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 联系我们: