Simple question about 'static' functions

Discussion in 'Programmer's Corner' started by dsp_redux, Jul 28, 2010.

  1. dsp_redux

    Thread Starter Active Member

    Apr 11, 2009
    182
    5
    Hi,

    Lets say I have a "super special" function written in C that I want static. I'm using gcc (or avr-gcc to be more precise). Here is the "super special" code:
    head.h
    Code ( (Unknown Language)):
    1.  
    2. static void display_A(void);
    3.  
    source.c
    Code ( (Unknown Language)):
    1.  
    2. #include "head.h"
    3.  
    4. static void display_A(void){
    5. //instructions...
    6. }
    7.  
    Here is what avr-gcc gives me with all it's wisdom:
    head.h:12: warning : 'display_A' declared 'static' but never defined
    It shows this warning for all my static functions. If I compile a second time, avr-gcc doesn't bring up those warnings again. My question is: Why? And what am I doing wrong? My supposition is that when reading the first time the files, the compiler doesn't know about the static functions, but finds them after and create the objects file. The second time the linker did it's job so the compiler knows where the static function is declared. Am I right?
     
  2. someonesdad

    Senior Member

    Jul 7, 2009
    1,585
    141
    I suggest you first explain why you're using the static keyword. In C, it's intended to mean "use internal linkage". By your declaring the function static in a header file, it indicates you may not understand what this means. Thus, please tell us first what you're trying to accomplish by declaring the function static.

    If you remove the declaration from the header file, the compiler warning will disappear (it's coming from the compilation of some other file that includes your header file). By declaring a function static, the programmer is saying it should only have file scope and not be callable outside the file. Thus, it doesn't make sense to put a declaration to a static function in a header file. The compiler warning is telling you that you've done something suspicious.
     
    dsp_redux likes this.
  3. dsp_redux

    Thread Starter Active Member

    Apr 11, 2009
    182
    5
    I solved my problem. The thing is, my other static functions were declared AFTER the calling function, not BEFORE. So I had warning in both cases. I removed the declaration from the header files and now everything is fine with the static functions before the calling (parent) one. Thanks for the hints!
     
Loading...