Unused function warning
I have a method in a header file that I have added a keyword to static
. eg.
static int32_t Tlvlist_AddRawt(Tlvlist *a, uint8_t type, uint16_t size, const void *bytes);
The method is implemented in a .c file where the static keyword is missing from the function name.
This method is called from another function in the same .c file. A later function (using this static function) is also called from main.
But I get a warning: "Unused function" Tlvlist_AddRawt "in the header file. Why is this happening?
ps. I am using Xcode.
When you mark a function declaration as static, it does not appear outside the translation unit in which it appears. But it also represents a different function in every translation unit in which it appears. Generally, the idea static
in a header file is rarely used because then you declare a separate function in each C source that includes the header.
The compiler diagnostics tells you that there is at least one C file that contains your header but does not contain a definition Tlvlist_AddRawt()
to go with the declaration from the header.
If you want to declare a static function separately from its definition - for example, prototype it for other functions that appear earlier in the source file - then place the declaration at the top of the C source file in which its body appears instead of the header. Putting it in the title is counterproductive.
source to share
You never declare functions static
in a header file for use in other modules, because the purpose of creating a function static
"hides" it from users outside of your modules. C static functions are visible only within the translation unit * where they are defined. When a function is declared static, but no other functions from the same translation unit use it, you get an unused static state warning.
If you want to define a function in one file and use it from another file, you need to place its direct declaration in a header, include that header from both translation units, and link the translation results together. Removing the keyword static
from the title should fix this problem.
* Translation module - fancy .c file name.
source to share