警告:内置函数“xyz”不兼容的隐式声明

时间:2021-10-30 06:58:23

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:

在与此警告相关的C文件的顶部,除了使用以下标志进行编译:

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 4.1.2:

$ gcc --version
gcc (GCC) 4.1.2 20080704

What should I do to resolve these warnings?

我应该怎样做才能解决这些警告?

4 个解决方案

#1


239  

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.

在C中,使用之前未声明的函数构成了函数的隐式声明。在隐式声明中,如果我没记错的话,返回类型为int。现在,GCC对一些标准函数有内置的定义。如果隐式声明与内置定义不匹配,则会得到此警告。

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.

要解决这个问题,您必须先声明函数,然后再使用它们;通常,您可以通过包含适当的头来实现这一点。如果可能的话,我建议不要使用-fno-builtin-*标志。

Instead of stdlib.h, you should try

而不是stdlib。h,你应该试一试

#include <string.h>

That's where strcpy and strncpy are defined, at least according to the strcpy(2) man page.

这就是strcpy和strncpy的定义,至少根据strcpy(2)手册页的定义。

The exit function is defined in stdlib.h, though, so I don't know what's going on there.

退出函数在stdlib中定义。但是,我不知道那里发生了什么。

#2


17  

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.

在编译程序phrap(例如)时,我得到了这些错误消息。这个程序碰巧包含修改或替换一些内置函数的代码,当我包含适当的头文件来修复警告时,GCC会生成一堆错误。因此,修复警告有效地破坏了构建。

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.

如果您将源代码作为应该正常编译的分布的一部分,那么错误可能是正常的。请参阅文档以确定。

#3


11  

Here is some C code that produces the above mentioned error:

以下是产生上述错误的C代码:

int main(int argc, char **argv) {
  exit(1);
}

Compiled like this on Fedora 17 Linux 64 bit with gcc:

像这样在Fedora 17 Linux 64位上编译,与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>

#4


6  

I met these warnings on mempcpy function. Man page says this function is a GNU extension and synopsis shows:

我在孟斐斯函数上遇到了这些警告。Man page说这个函数是一个GNU扩展和概要显示:

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

在#include之前,当#define被添加到我的源代码时,对GNU扩展的声明会被显示出来,并且警告会消失。

#1


239  

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.

在C中,使用之前未声明的函数构成了函数的隐式声明。在隐式声明中,如果我没记错的话,返回类型为int。现在,GCC对一些标准函数有内置的定义。如果隐式声明与内置定义不匹配,则会得到此警告。

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.

要解决这个问题,您必须先声明函数,然后再使用它们;通常,您可以通过包含适当的头来实现这一点。如果可能的话,我建议不要使用-fno-builtin-*标志。

Instead of stdlib.h, you should try

而不是stdlib。h,你应该试一试

#include <string.h>

That's where strcpy and strncpy are defined, at least according to the strcpy(2) man page.

这就是strcpy和strncpy的定义,至少根据strcpy(2)手册页的定义。

The exit function is defined in stdlib.h, though, so I don't know what's going on there.

退出函数在stdlib中定义。但是,我不知道那里发生了什么。

#2


17  

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.

在编译程序phrap(例如)时,我得到了这些错误消息。这个程序碰巧包含修改或替换一些内置函数的代码,当我包含适当的头文件来修复警告时,GCC会生成一堆错误。因此,修复警告有效地破坏了构建。

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.

如果您将源代码作为应该正常编译的分布的一部分,那么错误可能是正常的。请参阅文档以确定。

#3


11  

Here is some C code that produces the above mentioned error:

以下是产生上述错误的C代码:

int main(int argc, char **argv) {
  exit(1);
}

Compiled like this on Fedora 17 Linux 64 bit with gcc:

像这样在Fedora 17 Linux 64位上编译,与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>

#4


6  

I met these warnings on mempcpy function. Man page says this function is a GNU extension and synopsis shows:

我在孟斐斯函数上遇到了这些警告。Man page说这个函数是一个GNU扩展和概要显示:

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

在#include之前,当#define被添加到我的源代码时,对GNU扩展的声明会被显示出来,并且警告会消失。