Function defined but not used warning in C
"Function defined but not used" warning is only issued for functions with internal linkage, i.e. functions that are declared as static
. These functions are only accessible in one translation unit, so the compiler always knows whether they are used (in the program) or not. If you don't reference these functions in their translation unit, these functions are known to be unused, and the warning is generated.
You are saying that these functions "are not used in a.c, but used in b.c". This is not true. When you declare (and define) a function as static
in the header file, each translation unit that includes that header file gets its own internal copy of the function. Even though these functions look absolutely the same, they are still separate, completely independent functions. The fact that they have the same name and consist of the same code means nothing to the compiler. So, in b.c
you got a completely independent copy of the function, which is used (as you say), but the completely independent copy in a.c
is still not used.
The question in this case is why you are doing this. Why on Earth are you defining static functions in the header file? If you really need to do this (i.e. if you really want to spawn a separate internal "clone" of this function in each translation unit), you can work around the warning by using some compiler-specific means. Like in GCC, for example, you can declare the function with __attribute__((unused))
an the warning for this function will no longer be issued.
But normally one wouldn't need to define functions in the header file. Normally one'd use a function with external linkage (i.e. no static
), define it in one of the .c files and put the declaration (prototype) in the header file. The compiler will not issue any warnings in this case, even if the function is declared but not used in some translation unit.
If you just want to hide the warning, use:
-Wno-unused-function
However, you should probably follow the advice in caf's answer instead. It sounds like you may have defined a function when you only meant to add its declaration.