28

簡単なコードがありますが、警告が表示されます。

-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);

この警告を取り除く方法。

4

3 に答える 3

46

です#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-04T03:17:02.900 に答える
9

あなたは間違いを犯しやすいです.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);

これはすべて非標準関数であり、エラーがないことも説明しています。適切なリファレンスを見つけるのに苦労していますが、以前はstring.hも含まれていたBSDシステム バージョンのstrings.hです。

于 2013-11-04T03:25:20.477 に答える
0

コードは次のとおりで#include <stdlib.h>あり、私の出力は次のとおりです。

ここに画像の説明を入力

解決:

に変更stdlib.hされstdio.h、警告がなくなりました

つまり、コンパイラは、関数の宣言が見つからなかったことを伝えようとしています。これは a) の結果です。ヘッダー ファイルが含まれていません b) 間違ったヘッダー ファイル名 .eg" sring.h"

于 2016-08-06T22:12:44.210 に答える