gpt4 book ai didi

c - 为什么会出现这个错误? "static declaration of function follows non-static declaration"?

转载 作者:行者123 更新时间:2023-11-30 16:38:11 24 4
gpt4 key购买 nike

我正在学习静态函数,并且按照规则,如果我将函数声明为静态函数,那么我无法在其他 c 文件中访问该函数,如果我尝试访问,则应该出现错误“对 `fun 的 undefined reference ”所以我在 add.c 和 add.h 文件中声明并定义静态函数,并将该函数调用到 main.c 文件中,但我收到不同的错误,即“‘fun’的静态声明遵循非静态声明强文字所以我的问题是为什么会出现这个错误???请原谅我糟糕的英语!!!!

/************** 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 - 为什么会出现这个错误? "static declaration of function follows non-static declaration"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47590345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com