Pithikos
Pithikos

Reputation: 20300

Is there a way to have static prototypes AND public ones in the same header file in C?

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?

Upvotes: 2

Views: 539

Answers (4)

IF the "static" function you want to define is short enough or cheap enough, you'll better define it as static inline and put its definition with its body (not only its declaration) in the *.h header file.

Upvotes: 1

vhallac
vhallac

Reputation: 13907

You don't put declarations of static functions in header files. Since they are local to the .c file that defines them, it doesn't make sense to export them from a header file.

There are two things you can do:

  1. Define the function before it is called in the c file (move definition of a() above main).
  2. Declare static function at the top of the C file (this is my personal choice). In your case, you move the declaration of a() to the top of main.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.

Upvotes: 4

AndersK
AndersK

Reputation: 36082

It makes no sense having static prototypes in a header, the reason for this is that static functions have file scope so no module outside can anyway access the functions.

Instead I would suggest you put static prototypes only in the .c file at the top of the file in the same file they are defined.

Upvotes: 3

littleadv
littleadv

Reputation: 20272

If you have the declaration of b() in main.h, why do you need it again in other.c that includes it? Other than that, it's all good, can't see any issue.

The important point to keep in mind about the "normal" functions is that all the forward declarations of the same function (b() in this case) must match, otherwise you'll have trouble (linking errors, compiler errors, what's not). In your case they don't match.

Unless of course b(); is an actual call to the function, but at least in the code you posted its out of any scope, thus is treated as a forward declaration.

Re the static forward declarations, they limit the visibility of the function to the compilation unit. So calling a() in other.c will not execute the a() implemented in main.c. That's the whole point of static in C.

Upvotes: 1

Related Questions