Joe.Z
Joe.Z

Reputation: 2745

Receiving warning "implicit declaration of function 'strlen'"

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:

   #include <string.h>

   size_t strlen(const char *s);

how to get rid of this warning.

Upvotes: 30

Views: 88892

Answers (3)

Peter Chaula
Peter Chaula

Reputation: 3711

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

enter image description here

Solution:

Changed stdlib.h to stdio.h and the warning was gone

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"

Upvotes: 0

sukhvir
sukhvir

Reputation: 5565

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_name on terminal to see the header required for that function

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

Upvotes: 54

Shafik Yaghmour
Shafik Yaghmour

Reputation: 158469

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

#include <strings.h>

instead of:

#include <string.h>

the posix header includes support for:

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 BSD systems version of strings.h used to also include string.h.

Upvotes: 9

Related Questions