Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving warning "implicit declaration of function 'strlen'"

Tags:

c

posix

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.

like image 403
Joe.Z Avatar asked Nov 04 '13 03:11

Joe.Z


People also ask

What is warning implicit declaration of function?

This error occurs because you are trying to use a function that the compiler does not understand. If the function you are trying to use is predefined in C language, just include a header file associated with the implicit function.

What is implicit declaration error in C?

Such an 'implicit declaration' is really an oversight or error by the programmer, because the C compiler needs to know about the types of the parameters and return value to correctly allocate them on the stack.

What does strlen return?

The strlen() function determines the length of string excluding the ending null character. The strlen() function returns the length of string . This example determines the length of the string that is passed to main() .


2 Answers

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 
like image 162
sukhvir Avatar answered Sep 24 '22 03:09

sukhvir


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.

like image 30
Shafik Yaghmour Avatar answered Sep 25 '22 03:09

Shafik Yaghmour