静态声明遵循非静态声明,但没有静态声明的函数

Static declaration follows non-static declaration, but no functions declared statically

我有类似下面的代码:

void interact();
int main( int arg_count, const char* args[] ){
    tty_save();                 /* save current terminal mode */
    interact();                 /* interact with user */
    tty_restore();              /* restore terminal to the way it was */
    return 0;
}

void interact(){
    /* do various stuff */
}

我没看到 "static declaration is" 在哪里。我在 main() 之前(非静态地)声明一次函数,然后在 main() 之后再一次非静态地定义函数。为什么会出现此编译错误?

错误是"Static declaration of interact() follows non-static declaration"。

这种类型的错误经常发生在无意或故意将一个函数包含在另一个函数中时,这在 C 中通常是不允许的。经常发生这种情况是因为文件本身或文件中存在不匹配的前导括号通过 headers 包含的同一翻译单元中的其他文件之一。不匹配的大括号可能出现在包含的原型中,也可能出现在定义的 body 中。例如,如果您在原型中有语法错误,如下所示:

void stringInsertChars( BUFFER* buffer, const char* c ){

而不是...

void stringInsertChars( BUFFER* buffer, const char* c );

那么它可能会导致错误,这本身就是一种误导。在这里,发生的事情是从定义中复制了原型,但编码人员忘记将 { 更改为 ;

检测此问题的方法是仔细检查整个 body 错误消息。如果不匹配的大括号是问题所在,那么您将看到这样的错误:

File mycode.c in function stringInsertChars 46:
    Static declaration of interact() follows non-static declaration....
    (more errors of the same type)

因此,错误消息会告诉您哪个原型(或定义)有额外的大括号。