1

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
$ 
Digital Trauma
  • 15,475
  • 3
  • 51
  • 83

3 Answers3

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.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • @AlecTeal - Why not? - there is even a badge to encoarage exactly that: http://stackoverflow.com/help/badges/805/sportsmanship – Digital Trauma Nov 14 '13 at 19:56
0

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]
Community
  • 1
  • 1
Digital Trauma
  • 15,475
  • 3
  • 51
  • 83
-1

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.

Alec Teal
  • 5,770
  • 3
  • 23
  • 50
  • This is useful, but what I'm specifically looking for is the exact list of functions, be it some gcc or other tool output, or documentation – Digital Trauma Nov 14 '13 at 19:28
  • @DigitalTrauma the entire C standard library. Just include every heaver file, and have GCC spit out a list of declarations. You can compile with some flags that tell it not to use the standard library (say if you're making a kernel....) btw. – Alec Teal Nov 14 '13 at 19:29
  • Downvote not mine. But the answer I just found is not quite the same as yours. – Digital Trauma Nov 14 '13 at 19:36
  • @DigitalTrauma it knows about them all, that is GCCs job, but it might not warn about some of the more exotic / less commonly used ones because you don't use them as much. The standard doesn't specify how / when a compiler should warn, the fact it does is because GCC is a very useful tool. – Alec Teal Nov 14 '13 at 19:39
  • @ErnestFriedman-Hill see above. GCC knows of them all, but it obviously wont warn on them all. It can be really smart, and is really helpful, if it sees you using a bunch of functions commonly used together if used at all, it may not warn you if you use just one, but if you use the group it may. – Alec Teal Nov 14 '13 at 19:40
  • 2
    "*You're compiling C which allows implicit declarations*" -- Not quite true. C90 allows implicit declarations (and assumes that the called function returns `int`), but C99 does not. As of C99, calling a function with no visible declaration is a *constraint violation*, requiring a diagnostic. By default, gcc compiles C90 with GNU-specific extensions. You can use `-std=...` to change that. – Keith Thompson Nov 14 '13 at 19:42
  • @KeithThompson exactly. C became a lot more type safe in C99 (and beyond). – Alec Teal Nov 14 '13 at 19:43
  • You say "exactly", but your answer still says that C "allows implicit declarations". You might want to update your answer to clarify that point. – Keith Thompson Nov 14 '13 at 19:45
  • If someone says C++, which one do you assume? If someone says C we all assume C90 with GNU extensions, even Clang has the GNU extensions. It's also implied by the OPs question, if it wern't c90 he wouldn't get the same output. This is being overly pedantic. – Alec Teal Nov 14 '13 at 19:47
  • 1
    @AlecTeal: If you reply to someone, please include the person's name preceded by an at sign; otherwise they won't be notified of your reply. I only saw your comment by chance. No, we do not "all" assume that C means C90 with GNU extensions. The current official version of C is defined by the 2011 ISO standard. Implicit function declarations were removed from the language in 1999, and have *always* been a bad idea. This is not pedantry, it's just accuracy. – Keith Thompson Nov 15 '13 at 21:00
  • @KeithThompson you should post an answer stating that these warnings are wrong. – Alec Teal Nov 15 '13 at 21:18
  • @AlecTeal: Who says they're wrong? The "implicit declaration" warning is perfectly valid for either C90 or C90 plus GNU extensions. But C covers more territory than that. – Keith Thompson Nov 15 '13 at 21:26
  • @KeithThompson you are being overly (and annoyingly) pedantic. If I fought with someone you don't go "do you mean used a person as a weapon, or fought against a person" - the context helps; the rest is common sense. – Alec Teal Nov 15 '13 at 21:27