Simple question about 'static' functions

Thread Starter

dsp_redux

Joined Apr 11, 2009
182
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
Rich (BB code):
static void display_A(void);
source.c
Rich (BB code):
#include "head.h"

static void display_A(void){
//instructions...
}
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?
 

someonesdad

Joined Jul 7, 2009
1,583
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.
 

Thread Starter

dsp_redux

Joined Apr 11, 2009
182
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!
 
Top