cdaemonunistd.himplicit-declaration

warning: implicit declaration of function 'daemon'


I get this warning while compiling my program. daemon() is declared in unistd.h and its included. How to fix this or make it disappear?

error:

dcron.c: In function 'main':
dcron.c:35:4: warning: implicit declaration of function 'daemon' [-Wimplicit-function-declaration]
    if (daemon(1, 0) != 0) {
    ^

part of program:

if (daemon(1, 0) != 0) {
   fprintf(stderr, "error: failed to daemonize\n");
   syslog(LOG_NOTICE, "error: failed to daemonize");
   return 1;
}

setup: gcc4.8.2, glibc2.19 CFLAGS=-std=c99 -Wall -Wpedantic -Wextra


Solution

  • On Linux daemon() is made available by #defineing either

    by doing

    #define _XOPEN_SOURCE 
    

    or

    #define _BSD_SOURCE 
    

    before #includeing

    #include <unistd.h>
    

    or by adding -D _XOPEN_SOURCE or -D _BSD_SOURCE to the compilation command.