C语言 如何解决静态声明遵循 GCC C 代码中的非静态声明?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3148244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:46:35  来源:igfitidea点击:

How to solve static declaration follows non-static declaration in GCC C code?

cgcc

提问by Alsciende

I'm trying to compile the same C file on two different machines with different versions of cc.

我试图在两台不同版本的 cc 机器上编译同一个 C 文件。

gcc version 3.2.3 says warning: 'foo' was declared implicitly 'extern' and later 'static'

gcc 版本 3.2.3 说 warning: 'foo' was declared implicitly 'extern' and later 'static'

gcc version 4.1.2 says error: static declaration of 'foo' follows non-static declaration

gcc 版本 4.1.2 说 error: static declaration of 'foo' follows non-static declaration

Both have the same CFLAGS. I'd like to make gcc 4.1.2 behave like gcc 3.2.3, that is, find an option that would turn this error into a mere warning.

两者都有相同的 CFLAGS。我想让 gcc 4.1.2 表现得像 gcc 3.2.3,也就是说,找到一个选项,将这个错误变成一个纯粹的警告。

采纳答案by hlovdal

From what the error message complains about, it sounds like you should rather try to fix the source code. The compiler complains about difference in declaration, similar to for instance

从错误消息所抱怨的内容来看,您似乎应该尝试修复源代码。编译器抱怨声明的差异,类似于例如

void foo(int i);
...
void foo(double d) {
    ...
}

and this is not valid C code, hence the compiler complains.

这不是有效的 C 代码,因此编译器会抱怨。

Maybe your problem is that there is no prototype available when the function is used the first time and the compiler implicitly creates one that will not be static. If so the solution is to add a prototype somewhere before it is first used.

也许您的问题是第一次使用该函数时没有可用的原型,并且编译器隐式地创建了一个不是静态的原型。如果是这样,解决方案是在首次使用原型之前在某处添加原型。

回答by bjg

While gcc 3.2.3 was more forgiving of the issue, gcc 4.1.2 is highlighting a potentially serious issue for the linking of your program later. Rather then trying to suppress the error you should make the forward declaration match the function declaration.

虽然 gcc 3.2.3 对这个问题更宽容,但 gcc 4.1.2 强调了以后链接程序的潜在严重问题。而不是试图抑制错误,您应该使前向声明与函数声明匹配。

If you intended for the function to be globally available (as per the forward declaration) then don't subsequently declare it as static. Likewise if it's indented to be locally scoped then make the forward declaration static to match.

如果您打算将该函数全局可用(根据前向声明),则不要随后将其声明为静态。同样,如果它缩进为局部范围,则使前向声明静态以匹配。

回答by el.pescado

Try -Wno-traditional.

试试-Wno-traditional

But better, add declarations for your static functions:

但更好的是,为您的静态函数添加声明:

static void foo (void);

// ... somewhere in code
    foo ();

static void foo ()
{
    // do sth
}

回答by raggot

I have had this issue in a case where the staticfunction was called before it was declared. Moving the function declaration to anywhere above the call solved my problem.

static函数被声明之前被调用的情况下,我遇到了这个问题。将函数声明移动到调用上方的任何位置解决了我的问题。

回答by Boffin

This error can be caused by an unclosed set of brackets.

此错误可能是由一组未闭合的括号引起的。

int main {
  doSomething {}
  doSomething else {
}

Not so easy to spot, even in this 4 line example.

即使在这个 4 行示例中,也不容易发现。

This error, in a 150 line main function, caused the bewildering error: "static declaration of ‘savePair' follows non-static declaration". There was nothing wrong with my definition of function savePair, it was that unclosed bracket.

这个错误,在一个 150 行的 main 函数中,导致了令人困惑的错误:“'savePair' 的静态声明遵循非静态声明”。我对函数 savePair 的定义没有错,就是那个未闭合的括号。

回答by RotatingWheel

You have declared a function as non static in some file and you have implemented as static in another file or somewhere ion the same file can cause this problem also. For example the following code will produce this error.

您已在某个文件中将函数声明为非静态函数,并且在另一个文件或某个地方实现为静态函数,同一文件也可能导致此问题。例如下面的代码会产生这个错误。

void inlet_update_my_ratio(object_t *myobject);
//some where the implementation is like this
static void inlet_update_my_ratio(object_t *myobject) {
//code
}

If you remove the static from the implementation, the error will go away as below.

如果您从实现中删除静态,错误将消失,如下所示。

 void inlet_update_my_ratio(object_t *myobject) {
    //code
    }

回答by Nnaik

I had a similar issue , The function name i was using matched one of the inbuilt functions declared in one of the header files that i included in the program.Reading through the compiler error message will tell you the exact header file and function name.Changing the function name solved this issue for me

我有一个类似的问题,我使用的函数名称与我在程序中包含的头文件之一中声明的内置函数之一相匹配。阅读编译器错误消息将告诉您确切的头文件和函数名称。更改函数名称为我解决了这个问题