Is there a way to get warned about unused functions?

后端 未结 6 1591
执笔经年
执笔经年 2020-12-01 06:26

I\'d like to find unused functions in a codebase - including across compilations units. I\'m using gcc as my compiler.

Here\'s an example:

foo.c

6条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 06:42

    I know you asked for warnings and prefers not to use gcc option but it is really easy.

    You can use linker optimization (--gc-sections) in order to remove the dead code from your application.

    From gcc's man page:

    --gc-sections --no-gc-sections Enable garbage collection of unused input sections. It is ignored on targets that do not support this option. The default behaviour (of not performing this garbage collection) can be restored by specifying --no-gc-sections on the command line.

    --gc-sections decides which input sections are used by examining symbols and relocations. The section containing the entry symbol and all sections containing symbols undefined on the command-line will be kept, as will sections containing symbols referenced by dynamic objects. Note that when building shared libraries, the linker must assume that any visible symbol is referenced. Once this initial set of sections has been determined, the linker recursively marks as used any section referenced by their relocations. See --entry and --undefined.

    This option can be set when doing a partial link (enabled with option -r). In this case the root of symbols kept must be explicitly specified either by an --entry or --undefined option or by a "ENTRY" command in the linker script.

提交回复
热议问题