我有一个简单的代码,但我收到一个警告:
-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'
代码如下:
#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);
......
strlen()
的定义是:
#include <string.h>
size_t strlen(const char *s);
如何摆脱这个警告。
发布于 2013-11-04 11:17:03
我是#include <string.h>
。您在代码中拼写错误。另外,如果您的编译器中出现了该警告..始终在终端上执行man function_name
以查看该函数所需的标头
#include <string.h> // correct header
#include <strings.h> // incorrect header - change this in your code to string.h
发布于 2013-11-04 11:25:20
您被一个容易出错的错误所困扰,您包含了posix strings.h标头:
#include <strings.h>
而不是:
#include <string.h>
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);
这些都是非标准函数,这也解释了为什么没有错误,我很难找到一个很好的参考,但是BSD系统版本的string.h。h过去也包括string.h.h。
发布于 2016-08-07 06:12:44
原来代码是#include <stdlib.h>
,我的输出是:
解决方案:
将stdlib.h
更改为stdio.h
,警告已消失
简而言之,编译器试图告诉您它找不到函数的声明。这是一个)的结果。不包含头文件b)错误的头文件名.eg“sring.h”
https://stackoverflow.com/questions/19761104
复制相似问题