Search code examples
cgccstandard-librarygcc-warning

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


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
$ 

Solution

  • 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.