Reputation: 606
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
Reputation: 70911
On Linux daemon()
is made available by #define
ing either
_XOPEN_SOURCE
_BSD_SOURCE
by doing
#define _XOPEN_SOURCE
or
#define _BSD_SOURCE
before #include
ing
#include <unistd.h>
or by adding -D _XOPEN_SOURCE
or -D _BSD_SOURCE
to the compilation command.
Upvotes: 2
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