Search code examples
cposix

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.


Solution

  • 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