为什么会出现这个错误? "static declaration of function follows non-static declaration"?
why this error has come ? "static declaration of function follows non-static declaration"?
我正在学习静态函数,如果我将函数声明为
静态函数然后我无法将此函数访问到其他 c 文件,如果我尝试访问则应该出现错误“undefined reference to `fun”
所以我在 add.c 和 add.h 文件中声明和定义静态函数,并将该函数调用到 main.c 文件中,但是我得到了不同的错误,即“ [=20 的静态声明=] 遵循非静态声明"strong text
所以我的问题是为什么会出现这个错误????
请原谅我糟糕的英语!!!!
/************** main.c****************/
#include <stdio.h>
#include <stdlib.h>
#include "add.h"
int main(void)
{
printf("%d ", fun());
printf("%d ", fun());
return 0;
}
/***************add.c*************/
#include <stdio.h>
#include "add.h"
static int fun(void)
{
int a=5,b=4;
return a+b;
}
/*********************add.h*************/
#ifndef ADD_H_
#define ADD_H_
static int fun(void);
#endif /* ADD_H_ */
静态变量或函数只能在声明它的文件中看到。
C11 标准
6.2.2.3
If the declaration of a file scope identifier for an object or a
function contains the storage-class specifier static, the
identifier has internal linkage.
我正在学习静态函数,如果我将函数声明为 静态函数然后我无法将此函数访问到其他 c 文件,如果我尝试访问则应该出现错误“undefined reference to `fun” 所以我在 add.c 和 add.h 文件中声明和定义静态函数,并将该函数调用到 main.c 文件中,但是我得到了不同的错误,即“ [=20 的静态声明=] 遵循非静态声明"strong text 所以我的问题是为什么会出现这个错误???? 请原谅我糟糕的英语!!!!
/************** main.c****************/
#include <stdio.h>
#include <stdlib.h>
#include "add.h"
int main(void)
{
printf("%d ", fun());
printf("%d ", fun());
return 0;
}
/***************add.c*************/
#include <stdio.h>
#include "add.h"
static int fun(void)
{
int a=5,b=4;
return a+b;
}
/*********************add.h*************/
#ifndef ADD_H_
#define ADD_H_
static int fun(void);
#endif /* ADD_H_ */
静态变量或函数只能在声明它的文件中看到。
C11 标准
6.2.2.3
If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static, the identifier has internal linkage.