> オリジナルのヘッダファイル head.h
の中で、関数を以下のように宣言したとします、
>
> static int my_func(int head);
>
> そして head.c ファイルの中で my_func
関数の実態を定義したとします。
> my_func 関数の中ではいくつかの
static
変数を宣言し使ったとします。
> この場合、上記のように head.h
関数の中での
my_func 関数のプロトタイプは
> extern ではなく static な関数としておかないと、my_func
関数の中で
> static な変数を使っているので、分割コンパイルなのでもし他のファイルから
> my_func 関数を呼び出しているのがあると
static 変数の値が意図したものとは
> 異なったものになってしまいますよね?
・my_func が static 宣言されていると、他のファイルから呼び出すことはできない。
・関数の static 宣言と、関数内の局所変数の static 宣言とは無関係。
head.c の中に、他のファイルから呼び出して欲しくない関数 my_func と
他のファイルから呼び出して欲しい関数 my_func2 の定義があったとすると、
次のように書くべきです。
--- head.h ---
#ifndef HEAD_H
#define HEAD_H
int my_func2(int head);
#endif
--- end of head.h ---
--- head.c ---
#include "head.h"
static int my_func(int head);
int my_func(int head) { ... }
int my_func2(int head) { ... }
--- end of head.c ---
--- file.c ---
#include "head.h"
void func(void) { int n = my_func2(0); }
--- end of file.c ---
|