Detection of unused function and code in C
Iam writing a program in C. Is there any way(gcc options) to identify the unused 开发者_Python百科code and functions during compilation time.
If you use -Wunused-function
, you will get warnings about unused functions. (Note that this is also enabled when you use -Wall
).
See http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html for more details.
gcc -Wall
will warn you about unused static
functions and some other types of unreachable code. It will not warn about unused functions with external linkage, though, since that would make it impossible to write a library.
No, there is no way to do this at compile time. All the compiler does is create object code - it does not know about external code that may or may not call functions you write. Have you ever written a program that calls main
? It is the linker that determines if a function (specifically, a symbol) is used in the application. And I think GCC will remove unused symbols by default.
精彩评论