可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
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.
回答2:
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.
回答3:
Try -Wno-traditional
.
But better, add declarations for your static functions:
static void foo (void);
// ... somewhere in code
foo ();
static void foo ()
{
// do sth
}
回答4:
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.
回答5:
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
}
回答6:
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.
回答7:
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