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.
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.
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.
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() .
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With