siva
siva

Reputation: 31

How to find dead code in C language Project with gcc compiler

I need to find the dead code(functions not used) in my "C" language Project(having multiple C files) with gcc compiler. Please let me know the gcc options to find the dead code. Your help will be greatly appreciated.

Upvotes: 1

Views: 2416

Answers (2)

Florian Weimer
Florian Weimer

Reputation: 33719

For unused static functions, see Ed King's answer.

For global functions you could try this: Build the project twice, once as usual and once with -ffunction-sections -Wl,--gc-sections (the first is a compiler flag, the second a linker flag). Then you can run nm on the generated binaries to obtain a list of symbols for both runs. The linker will have removed unused functions in the second run, so that is your list of dead functions.

This assumes a common target like ELF, the binutils linker, and that the final binaries are not stripped of their symbol table.

Upvotes: 4

Ed King
Ed King

Reputation: 1863

You can use the GCC compiler option -Wunused-function to warn you of unused static functions. I'm not sure how you would detect unused 'public' functions though, save for looking at the map file for functions that haven't been linked.

Upvotes: 1

Related Questions