warning: incompatible implicit declaration of built-in function ‘xyz’
Solution 1
In C, using a previously undeclared function constitutes an implicit declaration of the function. In an implicit declaration, the return type is int
if I recall correctly. Now, GCC has built-in definitions for some standard functions. If an implicit declaration does not match the built-in definition, you get this warning.
To fix the problem, you have to declare the functions before using them; normally you do this by including the appropriate header. I recommend not to use the -fno-builtin-*
flags if possible.
Instead of stdlib.h, you should try:
#include <string.h>
That's where strcpy
and strncpy
are defined, at least according to the strcpy
(2) man page.
The exit
function is defined in stdlib.h, though, so I don't know what's going on there.
Solution 2
In the case of some programs, these errors are normal and should not be fixed.
I get these error messages when compiling the program phrap (for example). This program happens to contain code that modifies or replaces some built in functions, and when I include the appropriate header files to fix the warnings, GCC instead generates a bunch of errors. So fixing the warnings effectively breaks the build.
If you got the source as part of a distribution that should compile normally, the errors might be normal. Consult the documentation to be sure.
Solution 3
Here is some C code that produces the above mentioned error:
int main(int argc, char **argv) {
exit(1);
}
Compiled like this on Fedora 17 Linux 64 bit with gcc:
el@defiant ~/foo2 $ gcc -o n n2.c
n2.c: In function ‘main’:
n2.c:2:3: warning: incompatible implicit declaration of built-in
function ‘exit’ [enabled by default]
el@defiant ~/foo2 $ ./n
el@defiant ~/foo2 $
To make the warning go away, add this declaration to the top of the file:
#include <stdlib.h>
Solution 4
I met these warnings on mempcpy
function. Man page says this function is a GNU extension and synopsis shows:
#define _GNU_SOURCE
#include <string.h>
When #define
is added to my source before the #include
, declarations for the GNU extensions are made visible and warnings disappear.
Related videos on Youtube
Alex Reynolds
Bioinformaticist, hobbyist iPhone developer, pug caregiver
Updated on April 17, 2020Comments
-
Alex Reynolds about 4 years
I'm getting a number of these warnings when compiling a few binaries:
warning: incompatible implicit declaration of built-in function ‘strcpy’ warning: incompatible implicit declaration of built-in function ‘strlen’ warning: incompatible implicit declaration of built-in function ‘exit’
To try to resolve this, I have added
#include <stdlib.h>
at the top of the C files associated with this warning, in addition to compiling with the following flags:
CFLAGS = -fno-builtin-exit -fno-builtin-strcat -fno-builtin-strncat -fno-builtin-strcpy -fno-builtin-strlen -fno-builtin-calloc
I am using GCC 4.1.2:
$ gcc --version gcc (GCC) 4.1.2 20080704
What should I do to resolve these warnings?
-
user2584401 almost 15 yearsCan you post the code that fails?
-
Alex Reynolds almost 15 yearsUnfortunately, I don't have permission to repost this code.
-
-
Alex Reynolds almost 15 yearsThanks, I have about 20 .c files to clean up and missed adding some headers.
-
Terry Wang over 10 yearsSolved my issue when trying to compile a C program to check virtualization type (Xen HVM/KVM/VMware/Bare Metal), cool! Thanks.
-
Matt Fletcher almost 10 yearsRead the question; he's already mentioned adding stdlib.h. But that's only good enough for the exit, not for the string functions.
-
Al-Baraa El-Hag over 3 yearsAlthough it did help me out here.