Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

gcc

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 implicitly '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.

like image 650
Alsciende Avatar asked Jun 30 '10 10:06

Alsciende


4 Answers

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.

like image 28
raggot Avatar answered Nov 09 '22 07:11

raggot


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.

like image 165
hlovdal Avatar answered Nov 09 '22 09:11

hlovdal


Try -Wno-traditional.

But better, add declarations for your static functions:

static void foo (void);

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

static void foo ()
{
    // do sth
}
like image 8
el.pescado - нет войне Avatar answered Nov 09 '22 08:11

el.pescado - нет войне


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.

like image 6
bjg Avatar answered Nov 09 '22 07:11

bjg