为什么“extern”声明不能处理C中的静态函数?

时间:2022-04-29 13:25:38

Suppose the code:

假设代码:

extern int foo(void);

static int foo(void)
{
        return 0;
}

Try to compile with GCC

尝试用GCC编译。

$ gcc -Wall -std=c99 1.c 
1.c:3:12: error: static declaration of ‘foo’ follows non-static declaration
1.c:1:12: note: previous declaration of ‘foo’ was here
1.c:3:12: warning: ‘foo’ defined but not used [-Wunused-function]

So, how can I declare static function?

那么,如何声明静态函数呢?

4 个解决方案

#1


18  

Why declaration by extern doesn't work with static functions?

为什么extern的声明不能处理静态函数?

Because extern indicates external linkage while static indicates internal linkage. Obviously, You cannot have both on same function.

因为外部表示外部链接,而静态表示内部链接。显然,你不可能同时拥有两个函数。

In simple words, when you use static on the function you tell the compiler please limit the scope of this function only to this translation unit and do not allow anyone to access this from another translation unit.
while function declarations are extern by default, when you specify extern explicitly you tell the compiler, Please allow everyone from other translation units to access this function.
Well obviously the compiler cannot do both.

简单地说,当您在函数上使用static时,您告诉编译器,请将这个函数的范围限制在这个翻译单元内,并且不允许任何人从另一个翻译单元访问它。虽然函数声明默认为extern,但当您显式地指定extern时,您将告诉编译器,请允许来自其他转换单元的所有人访问此函数。显然编译器不能同时做这两件事。

So make a choice, whether you want the function to be visible only in the translation unit or not.
If former make it static and forget extern. If latter just drop the static.

所以做一个选择,不管你想要的函数只在翻译单元中可见。如果前者是静态的,那么忘记了extern。如果后者只是放弃静态。

Good Read:
What is external linkage and internal linkage?

好的阅读:什么是外部链接和内部链接?

Though above Q is for C++, most of what is discussed applies to C as well.

虽然以上的Q是针对c++的,但是讨论的大部分内容也适用于C。

#2


3  

You declare it with static

使用静态声明

static int foo(void);

static int foo(void)
{
        return 0;
}

#3


1  

extern means the function is defined in a different translation unit (file). static means it is only used in the translation unit in which it is defined. The two are mutually exclusive.

extern表示函数在不同的转换单元(文件)中定义。静态意味着它只在定义它的翻译单元中使用。这两者是相互排斥的。

#4


-1  

Is n't it that static can be resolved with extern(al) linkage like File1.c

是否静态可以通过外部链接(如File1.c)来解决?

static int fx(void)
{
    return int;
}

File2.c

File2.c

extern int fx(void);

/*call */
fx();

#1


18  

Why declaration by extern doesn't work with static functions?

为什么extern的声明不能处理静态函数?

Because extern indicates external linkage while static indicates internal linkage. Obviously, You cannot have both on same function.

因为外部表示外部链接,而静态表示内部链接。显然,你不可能同时拥有两个函数。

In simple words, when you use static on the function you tell the compiler please limit the scope of this function only to this translation unit and do not allow anyone to access this from another translation unit.
while function declarations are extern by default, when you specify extern explicitly you tell the compiler, Please allow everyone from other translation units to access this function.
Well obviously the compiler cannot do both.

简单地说,当您在函数上使用static时,您告诉编译器,请将这个函数的范围限制在这个翻译单元内,并且不允许任何人从另一个翻译单元访问它。虽然函数声明默认为extern,但当您显式地指定extern时,您将告诉编译器,请允许来自其他转换单元的所有人访问此函数。显然编译器不能同时做这两件事。

So make a choice, whether you want the function to be visible only in the translation unit or not.
If former make it static and forget extern. If latter just drop the static.

所以做一个选择,不管你想要的函数只在翻译单元中可见。如果前者是静态的,那么忘记了extern。如果后者只是放弃静态。

Good Read:
What is external linkage and internal linkage?

好的阅读:什么是外部链接和内部链接?

Though above Q is for C++, most of what is discussed applies to C as well.

虽然以上的Q是针对c++的,但是讨论的大部分内容也适用于C。

#2


3  

You declare it with static

使用静态声明

static int foo(void);

static int foo(void)
{
        return 0;
}

#3


1  

extern means the function is defined in a different translation unit (file). static means it is only used in the translation unit in which it is defined. The two are mutually exclusive.

extern表示函数在不同的转换单元(文件)中定义。静态意味着它只在定义它的翻译单元中使用。这两者是相互排斥的。

#4


-1  

Is n't it that static can be resolved with extern(al) linkage like File1.c

是否静态可以通过外部链接(如File1.c)来解决?

static int fx(void)
{
    return int;
}

File2.c

File2.c

extern int fx(void);

/*call */
fx();