Knowing if a function will be called or not is an undecidable problem (analogous to the stop issue for Turing machines). As you can call a function through a pointer (as it is done by callbacks) you cannot actually determine if a function will be called until you actually take it out from the code and run the code.
Linker only links functions that are referenced in the code, and doesn't know if they belong to dead code that will not be called.
If you want to know all the referenced functions in your code, just grep
the output of nm(1)
command on all your .o
files, to get all the U
undefined references (this is the list of functions that must be externally linked to your code). This will list all the external references to functions that must be resolved by the linker. If your function is not there, then it is not used by that module. You can match this list with the list of external functions (the ones marked as T
in nm(1)
output) of the .o
files that you want to check (or shared objects .so
) and you'll see (as the linker does) which ones are published to the linker but not referenced in your code. Think twice, as this only represents a direct reference, you have to manage also for indirect references (your module asks for a function in another module, that finally asks for the function you are trying to check).
In case your functions are static
(only file visibility) just surround the function definition by a #if 0
directive, and you'll get if the function is being referenced somewhere.
I repeat, you cannot easily know if a function will be called in your code, you can know if it is referenced somewhere.
I don't know what are you trying to identify with this question, but you can run into the XY
problem instance (what you ask is not what you try to solve)
By the way, defined functions in .h
header files are commonly declared inline
by developers to optimice function call/return execution. For this reason, they will be inlined where they are used and so, no reference appears to them on linking, so you have to search for them in the code (with the added problem of being macro expanded, so you need to run the preprocessor first to find the references to those functions)