mahdiolfat
mahdiolfat

Reputation: 961

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?

Upvotes: 6

Views: 3076

Answers (3)

alexanius
alexanius

Reputation: 412

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.

Upvotes: 4

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215221

Because of harmful traditions carried forward by mainstream compilers, including the one you're using. Thankfully they usually have options to make it an error, like -Werror=implicit-function-declaration for gcc and compatible compilers.

Upvotes: 2

John Bode
John Bode

Reputation: 123458

The standard places very few requirements on an implementation regarding diagnostics:

5.1.1.3 Diagnostics

1     A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined. Diagnostic messages need not be produced in other circumstances.9)
9) The intent is that an implementation should identify the nature of, and where possible localize, each violation. Of course, an implementation is free to produce any number of diagnostics as long as a valid program is still correctly translated. It may also successfully translate an invalid program.

Upvotes: 0

Related Questions