63

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.

pankanaj
  • 903
  • 3
  • 10
  • 13
  • 1
    Move the forward declaration from the header to your source file ? – simonc Mar 28 '13 at 12:10
  • 25
    If a function is `static`, you're not supposed to put it in a header file (unless `inline`)... –  Mar 28 '13 at 12:11
  • @H2CO3 or private header – SomeWittyUsername Mar 28 '13 at 12:14
  • 1
    Remove the static keyword from function definition - it's redundant there. Though this should've worked anyway - I suppose the problem is elsewhere. – SomeWittyUsername Mar 28 '13 at 12:17
  • 3
    Also be careful about the `()` in C. In a *declaration* this means that the function may receive any type of arguments. In particular this is not considered to be a prototype of the function. For the *definition* it means a function that does receive no parameter at all. In C you should always use `(void)` instead. – Jens Gustedt Mar 28 '13 at 14:09

2 Answers2

62

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.

hazzelnuttie
  • 1,403
  • 1
  • 12
  • 22
  • I was having the same problem. Obvious I should only have the static declarations inside the .c file and not in the header file. Duh :D – AntonioCS Jul 01 '13 at 22:25
19

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: Static functions in C

Community
  • 1
  • 1
Ehsan
  • 1,338
  • 14
  • 13