Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "implicit declaration of function" just a warning?

This is NOT a question on how to resolve the "implicit declaration of function" warnings that appear in C programs, that has been answered many times already.

I understand that this is a compiler warning, what I'm wondering is why is this a warning rather than an error? If the compiler cannot see the function, what happens when the function is called at runtime? Does the linker ultimately resolve this issue? Or are we to assume that the behaviour of calling a function that produced such warning is unknown?

like image 263
mahdiolfat Avatar asked Jul 22 '16 01:07

mahdiolfat


People also ask

What is implicit declaration of function warning?

implicit declaration of function means that you are trying to use a function that has not been declared. In our example above, StartBenchmark is the function that is implicitly declared.

How do you declare a function explicitly?

You can declare a function as an explicitly defaulted function only if the function is a special member function and has no default arguments. For example: class B { public: int func() = default; // Error, func is not a special member function.

How do I get rid of werror?

Werror is a GCC argument, and you cannot remove it directly via ./configure. Otherwise, an option like --disable-error would show up in the help text.


1 Answers

why is this a warning rather than an error?

Because there are a lot of legacy code thet is written in such way. Compiler error will break it.

If the compiler cannot see the function, what happens when the function is called at runtime? Does the linker ultimately resolve this issue?

Let's look the example:

int main()
{
    foo();
    return 0;
}

When working the compiler generates its own function signature like int foo(...) and will use it. By the way it can lead to very curious errors. So the object file will contain the call of this function and it is ok. When you will try to link it you will get an error: undefined reference to `foo'. But if you have another module with foo definition, the linker will find it by name and link it.

Or are we to assume that the behaviour of calling a function that produced such warning is unknown?

As I said it can lead to some curious errors. Imagine that you have code like int i = foo() and foo is without signature. And in another module you have the following: int * foo(){...}. When building application in 64 bit mode you will put to i only 32 bit of a 64 bit pointer. So you may say in fact the behaviour of your program may be unknown.

like image 61
alexanius Avatar answered Oct 19 '22 15:10

alexanius