我有一些简单的代码,但我收到一个警告:
-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'
Run Code Online (Sandbox Code Playgroud)
以下是代码:
#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);
......
Run Code Online (Sandbox Code Playgroud)
定义strlen()是:
#include <string.h>
size_t strlen(const char *s);
Run Code Online (Sandbox Code Playgroud)
如何摆脱这个警告.
suk*_*vir 38
它是#include <string.h>.你在代码中拼错了.此外,如果您在编译器中收到警告,请始终man function_name在终端上查看该函数所需的标头
#include <string.h> // correct header
#include <strings.h> // incorrect header - change this in your code to string.h
Run Code Online (Sandbox Code Playgroud)
你被一个容易犯的错误所咬,你包括了posix strings.h标题:
#include <strings.h>
Run Code Online (Sandbox Code Playgroud)
代替:
#include <string.h>
Run Code Online (Sandbox Code Playgroud)
的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);
Run Code Online (Sandbox Code Playgroud)
这些都是非标准功能,这也解释了缺少错误,我很难找到一个很好的参考,但BSD系统版本的strings.h过去也包括string.h.