implicit declaration warning: What are the built-in functions?

£可爱£侵袭症+ 提交于 2021-02-08 07:44:59

问题


The question-asking interface is flagging many "Questions that may already have your answer", but I have attempted to do due diligence to check if any are asking exactly what I am here. My apologies if this is a duplicate.

Suppose I have the following incorrect program:

extern void undefined_function(void);
int main(int argc, char **argv)
{
    undefined_function();
    undeclared_function();
    exit(0);
}

Compiling with gcc gives:

$ gcc warnings.c 
warnings.c: In function ‘main’:
warnings.c:6:2: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
/tmp/ccVzjkvX.o: In function `main':
warnings.c:(.text+0x15): undefined reference to `undefined_function'
warnings.c:(.text+0x1f): undefined reference to `undeclared_function'
collect2: ld returned 1 exit status
$ 

I know why these warnings are emitted, and how to correct them - that is not my question.

From the output, it is clear that gcc is treating exit() differently to the other undefined/undeclared functions, because it considers it a "built-in function"

For a given gcc, how can I tell what the list of functions is that gcc considers to be "built-in functions"? Is it exactly the list of c standard library functions or something else?

I considered doing nm libc.so, but on my Ubuntu VM, this glibc appears to be stripped, so there is no useful information there in this regard:

$ nm /lib/x86_64-linux-gnu/libc.so.6
nm: /lib/x86_64-linux-gnu/libc.so.6: no symbols
$ 

回答1:


The list is quite long, and quite platform-specific. Many (but by no means all) of the functions in the C standard library are (sometimes) treated as built-ins. But there are also a raft of built-in functions that relate to specific processor instructions and other hardware features. They're documented in various pages linked from here; in particular, see here, here, here, here, and here.




回答2:


After digging through the gcc documentation a bit more, I think I found a reasonable partial answer to this (though this answer is more complete in its references):

http://gcc.gnu.org/onlinedocs/gcc-4.8.2/gcc/Other-Builtins.html#Other-Builtins

"GCC includes built-in versions of many of the functions in the standard C library" (emphasis mine). I take this to mean that most but not all standard library functions are built-in.

The documentation continues with several lists applying to various levels of the c standard:

"The ISO C90 functions abort, abs, acos, asin, atan2, atan, calloc, ceil, cosh, cos, exit, ..."


An example of a standard library function which is not a gcc builtin is bsearch(). If I add a call to this in the program, without #include <stdlib.h> and compile with -Wimplicit-function-declaration I get just:

warnings.c:5:2: warning: implicit declaration of function ‘bsearch’ [-Wimplicit-function-declaration]

Whereas for exit(), I get:

warnings.c:8:2: warning: implicit declaration of function ‘exit’ [-Wimplicit-function-declaration]
warnings.c:8:2: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]



回答3:


You're compiling C which allows implicit declarations, so your "exit" is declared implicitly. and the compiler trusts you to define it later.

Implicit declarations are fine, if you implied exit with the usual behaviors (the implied prototype was identical to the actual prototype) it'd be fine.

GCC can only tell you about built-in because you know that for a C compiler the stdlib will be there. Those are the only function it knows to exist (built in) to tell you you are implying wrongly

Otherwise I could have a header file on my desktop defining a prototype, and when compiling something somewhere else get a warning there about it. GCC would have to scan everywhere and that'd be undesirable.... so forth, you will only get an implicit warning like these if you implicitly define something and later in the translation unit or the actual version (or another implicit) disagree.

GCC knows about the stdlib = built in.

Addendum

GCC takes your word for it that the undefined functions wil exist, using the implict declaration thing, and nothing contradicts that , so it is fine. It doesn't know what undeclared_function should actually look like, but it knows what exit should look like.

Then as you know, the linker throws a fit (rightfully so of course) because it can't find them.



来源:https://stackoverflow.com/questions/19986247/implicit-declaration-warning-what-are-the-built-in-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!