开发者

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

开发者 https://www.devze.com 2023-01-05 18:05 出处:网络
I\'m trying to compile the same C file on two different machines with different versions of cc. gcc version 3.2.3 says warning: \'foo\' was declared implici开发者_如何学Pythontly \'extern\' and later

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

gcc version 3.2.3 says warning: 'foo' was declared implici开发者_如何学Pythontly 'extern' and later 'static'

gcc version 4.1.2 says 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.


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.

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.


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


Try -Wno-traditional.

But better, add declarations for your static functions:

static void foo (void);

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

static void foo ()
{
    // do sth
}


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.

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.


You have declared a function as nonstatic in some file and you have implemented as static in another file or somewhere in 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
    }


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.

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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消