C语言 收到警告“隐式声明函数‘strlen’”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19761104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:52:32  来源:igfitidea点击:

Receiving warning "implicit declaration of function 'strlen'"

cposix

提问by Joe.Z

I have a some simple code, but I am receiving a warning:

我有一些简单的代码,但我收到警告:

-bash-3.2$ gcc -Wall print_process_environ.c -o p_p
print_process_environ.c: In function 'print_process_environ':
print_process_environ.c:24: warning: implicit declaration of function 'strlen'
print_process_environ.c:24: warning: incompatible implicit declaration of built-in function 'strlen'

The following is the code:

以下是代码:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <strings.h>

    void
    print_process_environ(pid_t pid)
    {
        int     fd;
        char    filename[24];
        char    environ[1024];
        size_t  length;
        char    *next_var;

        snprintf(filename, sizeof(filename), "/proc/%d/environ", (int)pid);
        printf("length of filename: %d\n", strlen(filename));

        fd = open(filename, O_RDONLY);
......

The definition of strlen()is:

的定义strlen()是:

   #include <string.h>

   size_t strlen(const char *s);

how to get rid of this warning.

如何摆脱这个警告。

回答by sukhvir

it's #include <string.h>. You are spelling it wrong in your code. Also if you ever get that warning in your compiler .. always do man function_nameon terminal to see the header required for that function

它是#include <string.h>。您在代码中拼写错误。此外,如果您在编译器中收到该警告.. 总是man function_name在终端上查看该函数所需的标头

 #include <string.h> // correct header
 #include <strings.h> // incorrect header - change this in your code to string.h

回答by Shafik Yaghmour

You were bitten by a easy to make mistake, you included the posix strings.hheader:

你被一个容易犯的错误所困扰,你包含了posix strings.h头文件:

#include <strings.h>

instead of:

代替:

#include <string.h>

the posixheader includes support for:

POSIX报头包括用于支持:

int    bcmp(const void *, const void *, size_t); (LEGACY )
void   bcopy(const void *, void *, size_t); (LEGACY )
void   bzero(void *, size_t); (LEGACY )
int    ffs(int);
char  *index(const char *, int); (LEGACY )
char  *rindex(const char *, int); (LEGACY )
int    strcasecmp(const char *, const char *);
int    strncasecmp(const char *, const char *, size_t);

which are all non-standard functions, this also explains the lack of error, I am having a hard time finding a good reference but BSDsystems version of strings.hused to also include string.h.

这些都是非标准函数,这也解释了没有错误的原因,我很难找到一个好的参考,但BSD系统版本的strings.h曾经也包含string.h

回答by peterchaula

It turns out the code was #include <stdlib.h>and my output was :

原来代码是#include <stdlib.h>,我的输出是:

enter image description here

在此处输入图片说明

Solution:

解决方案:

Changed stdlib.hto stdio.hand the warning was gone

更改stdlib.hstdio.h并且警告消失了

In short, the compiler is trying to tell you that it couldn't find the declaration of a function. This is a result of a). No header file included b) wrong header file name .eg" sring.h"

简而言之,编译器试图告诉您它找不到函数的声明。这是a)的结果。不包含头文件 b) 错误的头文件名。例如“sring.h”