implicit declaration of function swab
Question
EDIT: the warning in the first paragraph was due to me reading the old log from the compiler (before including unistd.h, sorry for the missconfusion, the second problem still resides)
I have a code that makes use of the c function unlink (declared in unistd.h). Upon compilation with gcc -Wall I get the warning: implicit declaration of function ‘unlink’
Now I now that this is just a warning but it kinda annoys me and I would like what would be the proper way to resolve this. My current solution is to simply add the line extern int unlink(const char *path);
to the code but this seems a bit silly as I am also including unistd.h
I used to got a warning about the use of the function swab (also declared in unistd.h) but managed, after inspection of unistd.h, to resolve this by adding the lines
#ifndef __USE_XOPEN
#define __USE_XOPEN
#endif
Not sure that this is the proper thing to do so any insights to this would also be appreciated.
Solution
Referring your second issue ("implicit declaration of function sawb
"):
Replace
#ifndef __USE_XOPEN
#define __USE_XOPEN
#endif
with
#define _XOPEN_SOURCE
... placing it before all other #include
statements should make the prototyping for #include <unistd.h>
swab()
be available.
#define
s starting with a double underscore __
are reserved for internal use and shall not be set by a program directly.
OTHER TIPS
You most likely have a typo or something simple wrong.....try this.
#include <unistd.h>
int
main()
{
int r;
r = unlink("foo");
return(r);
}
Compile it
gcc -Wall foo.c
Above should be a clean compile. You can pre-process and do a grep to verify that unlink declaration is pulled in.
sys> gcc -E foo.c | grep unlink
int unlink(const char *);
r = unlink("foo");