0

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.

cpaitor
  • 423
  • 1
  • 3
  • 16

2 Answers2

2

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");
Arun Taylor
  • 1,574
  • 8
  • 5
  • Hmmmm, that compiled like a charm. You are probably correct in that there are some problems with the code, I did not write it my-self but am stuck with trying to clean up the mess – cpaitor Nov 15 '13 at 18:27
1

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 #include <unistd.h> should make the prototyping for swab() be available.


#defines starting with a double underscore __ are reserved for internal use and shall not be set by a program directly.

alk
  • 69,737
  • 10
  • 105
  • 255
  • Hmmm, no good, that just gave me the warning about implicit declaration of swab back – cpaitor Nov 15 '13 at 17:30
  • @cpaitor: Which gcc version and platform are you using? Also please see my updated answer. – alk Nov 15 '13 at 17:32
  • gcc 4.6.2, openuSuSe 12.1, I figured that `__` was not to be used, Oh I also updated my question as the unlink warning was not really there, sorry for that – cpaitor Nov 15 '13 at 17:46
  • 1
    @cpaitor: (and alk) You should put feature-test macro definitions (`#define _XOPEN_SOURCE`) before *any* #include, not just before `#include `. Otherwise it's possible that some other header includes the internal header which defines `swab`, and it will not be included again with the new feature-test setting. – rici Nov 15 '13 at 18:08
  • @rici: that did the trick. A follow up, do I need (or rather should I) to set the value of _XOPEN_SOURCE to some specific value? – cpaitor Nov 15 '13 at 18:24
  • 1
    @cpaitor: For swab, no. For other interfaces, possibly; the `man` page for the function call should tell you. (`500` enables Unix98, `600` enables Posix2001, and `700` enables Posix2008, assuming your standard library implements those interfaces.) – rici Nov 15 '13 at 18:46
  • @rici: Thanks for clarifing what I initially did not mention. I updated my answer accordingly. – alk Nov 15 '13 at 20:53