编译器删除未使用的函数,定义,变量,宏,包括等 [英] Do compilers remove unused functions, definitions, variables, macros, includes etc

查看:211
本文介绍了编译器删除未使用的函数,定义,变量,宏,包括等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,我们定义或声明函数,定义,变量,宏包括和数据结构。


  1. 这些未使用的资源会被编译器自动删除(现代编译器能够优化)?
  2. $ / p>

    宏由编译器转换为程序文本。他们
    不代表除了替换它们的文本之外的任何东西,
    并且不会超过编译时间(除非...见下文)。



    局部变量和这样的可能被删除,如果他们没有
    a非平凡的构造函数或析构函数。 (您不想
    scoped_lock 的删除只是因为您不是
    后来在代码中引用。)同样适用于变量
    和具有内部链接的函数(例如在关键字 static 的命名空间
    范围中定义)。然而,在大多数情况下,这样的对象所需的
    资源是最小的,并且节省
    可忽略。



    函数有点不同,并且依赖。一个虚函数
    一般被认为是used,如果有一个类型的实例
    ,它几乎肯定会出现,即使
    从未被调用。



    除此之外(这也适用于全局变量):
    到链接器。大多数链接器的粒度是对象
    文件,由编译翻译单元产生:
    目标文件是或不是你的程序的一部分。如果你
    告诉链接器合并对象文件,那么你应该
    得到它的所有内容。如果你把目标文件放在
    a(静态)库中,并告诉链接器使用它,那么
    链接器将把目标文件合并到程序if和
    中,否则未解决外部。但是如果
    合并了目标文件,它通常会包含它的所有
    。 (任何好的库会将每个非虚函数放在
    a单独的对象文件中,所以你不会得到你需要的更多。)



    ,DLL的行为像对象文件(尽管它们的
    名称)。如果你把你的目标文件链接到DLL,程序
    使用它将获得所有的DLL,或没有。



    最后:虽然不是一部分你的程序,目标文件和
    最终的可执行文件通常会包含符号信息;
    最好的系统甚至将维护有关
    宏的信息,以便调试器可以显示你
    写的方式。 (使用宏可以做到这一点有多大争议。)


    Some times when programming we define or declare functions, definitions, variables, macros includes and data structures. but never use those after that.

    1. Those unused resources automatically removes by the compiler (modern compilers capable of optimizing) ?
    2. Are there anyway to recognize those?

    解决方案

    It depends:

    Macros are converted to program text by the compiler. They don't represent anything other than the text that replaces them, and don't live beyond compile time (unless... see below).

    Local variables and such are likely removed, if they don't have a non-trivial constructor or destructor. (You don't want something like scoped_lock removed just because you don't reference it later in the code.) The same holds for variables and functions with internal linkage (e.g. defined at namespace scope with the keyword static). In most cases, however, the resources needed for such objects is minimal, and the savings negligible.

    Functions are a bit different, and depend. A virtual function will generally be considered "used" if there is ever an instance of its type, and it will almost certainly be present, even if it is never called.

    Beyond that (and this applies to global variables as well): it's up to the linker. The granularity of most linkers is the object file which results from compiling a "translation unit": that object file either is or is not part of your program. If you tell the linker to incorporate the object file, then you should get everything that is in it. If you put the object file in a (static) library, and tell the linker to use this, then the linker will incorporate the object file into your program if and only if it resolves an otherwise unresolved external. But if it incorporates the object file, it generally will incorporate all of it. (Any good library will put each non-virtual function in a separate object file, so you don't get more than you need.)

    In this regard, DLL's behave like object files (despite their name). If you link your object files into a DLL, the program which uses it will get all of the DLL, or none.

    Finally: although not part of your program, the object files and the final executable will often contain symbolic information; the best systems will even maintain information concerning macros, so that the debugger can display things the way you wrote them. (How far it can do this with macros is debatable.)

    这篇关于编译器删除未使用的函数,定义,变量,宏,包括等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆