Home > Net >  How does GCC compiler find the header file corresponding to some implicitly declared functions?
How does GCC compiler find the header file corresponding to some implicitly declared functions?

Time:12-30

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]

CodePudding user response:

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.

  • Related