Hello,
I've always assumed that a C compiler (like gcc) compiles all functions, including those that you never call. Is it so?
I'm thinking of a situation where I have a .h file containing three independent functions: funcA(), funcB(), and funcC(). In the main program I call the functions funcA() and funcB() but never the function funcC().
Is the compiler smart enough to not compile the unused function, i.e. leave it out from the object file, or do I have to use #ifdef and #define to exclude the unused function?
Things like that usually depend on your optimisation settings. check out the -o parameter for gcc for more information.
Simon
Rigo isn't that the output switch? I think you mean O. ;-)
I was almost caught by that last night before I checked the docs.
For me it seems a bad idea to have different functions for lower or upper options. Sure to a computer it's another number. But humans are inputting the information and it can look the same or similar enough before you discern the difference.
If all three functions are contained in a single .c file then the linker has no choice. It must include all of them if any of them is referenced.
Better put all three functions into distinct .c files. Then the linker is able to exclude those .o files which contain functions that are not referenced from anywhere.
I glanced through the gcc manual and it seems that the gcc compiler will always include all unused functions unless you do what tboeckel suggests or you exclude unused functions by using #ifdefs.
What I found was that the gcc has an option (-Wunused-function) which makes the compiler warn of unused static functions. So it's possible to get warnings on some unused functions but not all.
hello
Perhaps you can do that using inlining
I explain for inlined functions gcc got some option (name?) for not keepin a copy of (inlined) function but only to inline it where it is used
Alain Thellier
Alain Thellier - Wazp3D
I've just tested and whatever the optimization level, an unused global function won't be removed.
The option -Wuused-function will only warn for unused static functions. Note that a Linux tool like cppcheck should help to find unused global functions (it works parsing AmigaOS code).
For what you are looking for, the option -flto (link time optimization) will remove unused functions, but the compilation / link will be slower and you won't be informed about these unused functions.