If you compile into object files (rather than executable files), then the compiler will never delete any non- static functions, since you can always link an object file to another object file that will call this function. So, your first step is to declare as many static functions as possible.
Secondly, the only way for the compiler to remove any unused functions is to statically link your executable. In this case, there is at least the likelihood that the program can come and find out which functions are used and which of them are not used.
Trap, I don't believe gcc really does this type of cross-modular optimization. Itβs best to use the -Os flag to optimize code size, but even if you have an abc.o object file that has some unused non-static functions and you put a static link to some def.exe executable, t believe gcc will go and cross out the code for unused functions.
If you really need this to be done, I think that you probably should actually #include merge the files, so that after going through the preprocessor this will lead to the compilation of one .c file. With gcc compiling one monstrous jumbo source file, you have a chance to avoid unused functions.
source share