Reputation:
I have method in header file to which I added static
keyword. e.g.
static int32_t Tlvlist_AddRawt(Tlvlist *a, uint8_t type, uint16_t size, const void *bytes);
The method is implemented in .c file where the static keyword is not present in function name.
This method is called from another function of same .c file. The later function (which uses this static function) is also called from main.
But I get warning: "Unused function 'Tlvlist_AddRawt'" in the header file. Why would this happen?
ps. I use Xcode.
Upvotes: 3
Views: 6282
Reputation: 180103
When you mark a function declaration static, it is not visible outside the translation unit in which it appears. But also, it represents a different function in every translation unit in which it appears. As such, it is rarely a good idea to use static
in a header file, because then you're declaring a separate function in each C source that includes the header.
The compiler diagnostic is telling you that there is at least one C file that includes your header but does not provide a definition of Tlvlist_AddRawt()
to go with the declaration from the header.
If you want to declare a static function separately from its definition -- for instance to prototype it for other functions that appear earlier in the source file -- then put the declaration in at the top of the C source file in which its body appears instead of in a header. Putting it in a header is counterproductive.
Upvotes: 7
Reputation: 726489
You never declare static
functions in a header file intended for use in other modules, because the purpose behind making a function static
is "hiding" it from users outside your modules. Static C functions are visible only inside 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 the "unused static" warning.
If you would like to define a function in one file, and use it from another file, you need to put its forward declaration in a header, include that header from both translation units, and link the translation results together. Removing the static
keyword from the header should address this problem.
* Translation Unit is a fancy name for a .c file.
Upvotes: 4