The compiler will always whine when I have mixed public with static prototypes in a header file.
Say I have header main.h with a static prototype for function a and a normal prototype for function b. Then I want to #include the header in two .c files, namely main.c and other.c. In main.c I need an include as I call the function before any prototype.
main.h:
static int a(void);
int b(void);
main.c:
#include "main.h"
void main(){
a();
}
static int a(void){
/* do stuff */
}
int b(void){
/* do stuff */
}
other.c:
#include "main.h"
b();
What’s the best practice for this than the obvious solution of splitting the header file into a seperate header exclusively for static prototypes and one for public ones?
You don’t put declarations of static functions in header files. Since they are local to the
.cfile that defines them, it doesn’t make sense to export them from a header file.There are two things you can do:
a()above main).a()to the top ofmain.c.If the function is going to be used in multiple translation units, then you can define the static function in the header file. It could usefully be inline instead of static assuming you have a grown-up compiler that supports C99.