MS VS-2005 编译器优化不删除未使用/未执行的代码
我有一个使用 MS-Visual Studio 2005 构建的工作区,其中包含所有 C 代码。在其中我看到许多函数未被调用,但它们仍然被编译(它们不在任何编译时宏下以禁用它们编译)。
我为 MS-VS2005 项目设置了以下优化设置,以删除未使用的代码:-
优化级别 - /Ox
启用整个程序优化 - /GL
我尝试了 Favor speed /Ot 和 Favor Size /Os
尽管有所有这些选项,当我查看链接器生成的映射文件,我看到映射文件中存在符号(未使用 sed 函数)名称。
我错过了什么吗?我想完全删除未使用的代码。
我该怎么做?
I have a workspace built using MS-Visual Studio 2005 with all C code.In that i see many functions which are not called but they are still compiled(they are not under any compile time macro to disable them from compiling).
I set following optimization settings for the MS-VS2005 project to remove that unused code:-
Optimization level - /Ox
Enable whole program optimization - /GL
I tried both Favor speed /Ot and Favor Size /Os
Inspite of all these options, when i see the linker generated map file, I see the symbols(unsed functions) names present in the map file.
Am I missing something? I want to completely remove the unused code.
How do I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编译器一次编译一个 C 文件。因此,在编译包含未使用函数的 C 文件时,编译器无法确定它不会从另一个文件调用,因此它也会编译该函数。但是,如果该函数被声明为静态(文件范围),那么编译器会知道它没有被使用,因此将其删除。
即使进行了整个程序优化,我认为仍然无法完成,因为编译可能是针对库的。
链接器所做的事情与您正在寻找的类似。如果您的代码链接到包含多个对象的库,则任何不包含代码(直接或间接)使用的函数的对象都不会包含在最终的可执行文件中。
一种选择是将代码分成单独的库和目标文件。
PS-这只是我的猜测。编译器(具有整个程序优化)或链接器的行为本质上取决于特定编译器或链接器的设计选择
The compiler compiles C files one-at-a-time. Therefore, while compiling a C-file that does contains an unused function, the compiler cannot be sure that it will not be called from another file and hence it will compile that function too. However, if that function were declared as static (file-scope), then the compiler would know it is not used and hence remove it.
Even with whole program optimization, I think it would still not be done since the compilation could be for a library.
Linkers do something similar to what you are looking for. If your code links against a library containing multiple objects, then any objects that do not contain functions used by your code (directly or indirectly) would not be included in the final executable.
One option would be to separate your code into individual libraries and object files.
PS - This is just my guess. The behavior of the compiler (with whole program optimization) or linker essentially depends on the design choices of that particular compiler or linker
在我们的项目中,我们在项目属性\链接器\引用下设置了一个标志。我们将其设置为“消除未引用的数据”(/OPT:REF),根据描述,这应该删除函数调用或从未使用过的数据。我只是根据描述,我从未测试过或使用过它。但我只是在过去一个小时内碰巧看到了它,并认为它可能是你可以尝试的东西。
On our projects we have a flag set under the project properties\Linker\Refrences. We set it to Eliminate Unreferenced Data (/OPT:REF), according to the description this is supposed to remove function calls or data that are never used. I am just going by the description, I have never tested this or worked with it. But I just happened to see it within the last hour and figured it might be something you could try.