Is there a way to get warned about unused functions?

后端 未结 6 1604
执笔经年
执笔经年 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

    gprof is the simplest solution I guess. I compiled the sample program you've put up with -pg option so that we get the gmon.out when we run a.out (which gprof can use later) and then I finally ran gprof -z a.out gmon.out | tee output.txt . I could find your function foo in the unused list! i.e called 0 times. -z is the option you should use along with gprof to track unused routines.

    Thanks to this thread for the appropriate pointer!

    PS: gprof threw up a slew of other unused library functions along with your unused function foo. I seriously don't know how to filter this :)

提交回复
热议问题