0

In the case of an implicitly declared function, gcc will sometimes tell you the header file from which the function belongs. From this answer, it seems only some functions are built in - "some compilers contain built-in declarations for them so they can do some basic type checking".

Is this how gcc is able to tell you which header file corresponds to some implicitly declared functions and not others?

For example,

  • implicit printf usage will generate an additional comment:

    • compilation.c:4:5: note: include the header <stdio.h> or explicitly provide a declaration for 'printf'
  • but bsearch from stdlib does not:

    • compilation.c:5:5: error: implicit declaration of function 'bsearch' is invalid in C99 [-Werror|,-Wimplicit-function-declaration]
marquitos
  • 3
  • 2
  • I guess it depends on the C standard you're using. – Anic17 Dec 29 '21 at 19:28
  • Well, different standards may define different "built-in" functions, but for a given standard, how does the compiler find the header file associated with the implicitly declared "built-in" function? – marquitos Dec 29 '21 at 19:48
  • printf is such a common source of errors that it probably gets special treatment by the parts of the compiler that try to be nice to humans. Apart from that it is also an *intrinsic* (a [builtin function](https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html)), so gcc knows much more about it than the "normal" declaration would have you assume. – Peter - Reinstate Monica Dec 29 '21 at 19:51
  • The compiler, the headers, and the libraries come as a package. You can add additional headers and libraries, but the C standard prohibits changing or replacing the headers or libraries that come with the compiler. So the compiler knows that `printf` is in `stdio.h` because some developer wrote code to make that connection. – user3386109 Dec 29 '21 at 20:10
  • Thanks for the replies and clarification. @EricPostpischil good catch, I messed up the copy paste :) fixed it – marquitos Dec 29 '21 at 21:29

1 Answers1

1

how gcc is able to tell you which header file corresponds to some implicitly declared functions and not others?

Gcc has a list of symbols and headers. When the symbol is encountered and it is not defined and it is in the list, then a message is displayed with the proposed header name.

See the list at https://github.com/gcc-mirror/gcc/blob/16e2427f50c208dfe07d07f18009969502c25dc8/gcc/c-family/known-headers.cc#L157 from gcc sources.

bsearch is not in the list, so the hint is not displayed. I like the hints, it would be nice for me to include all the symbols from C standard, including bsearch. It would also be a speedup if the list would be sorted and would use bsearch. You can contribute to gcc or donate to gcc and write about it to gcc mailing list.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111