function declared static but never defined

CGcc Warning

C Problem Overview


I have a header file suppose abc.h, where i have function declaration as:

static int function1();

I have included this header file in abc.c and has defined the function and used it.

static int function1()
{
 < function definition>
}

After compiling I am getting warning:

warning: function1 declared static but never defined

How can I remove warning, without removing static. Thanks.

C Solutions


Solution 1 - C

A static function can be declared in a header file, but this would cause each source file that included the header file to have its own private copy of the function, which is probably not what was intended.

Are u sure u haven't included the abc.h file in any other .c files?

Because declaring a function as static, requires the function to be defined in all .c file(s) in which it is included.

Solution 2 - C

Good practice: Declare static functions in the source file they are defined in (please also provide prototype), since that's the only file they are visible in.

This way, the function is only visible to that file, such visibility issues can reduce possible code conflict! So, just provide the prototype and the static function definition in the .c file. Do not include the static function in the header file; the .h file is for external consumption.

Duplicate: https://stackoverflow.com/questions/14149814/static-functions-in-c/41069834#41069834

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpankanajView Question on Stackoverflow
Solution 1 - ChazzelnuttieView Answer on Stackoverflow
Solution 2 - CEhsanView Answer on Stackoverflow