Ari Malinen
Ari Malinen

Reputation: 606

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

Upvotes: 1

Views: 3161

Answers (2)

alk
alk

Reputation: 70911

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

  • _XOPEN_SOURCE
  • _BSD_SOURCE

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.

Upvotes: 2

ouah
ouah

Reputation: 145829

You need to add the relevant header file and enable the _BSD_SOURCE feature test macro:

#define _BSD_SOURCE
#include <unistd.h>

From man 3 daemon:

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

daemon(): _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)

Upvotes: 3

Related Questions