5

I'm trying to build my library but a file called evutil.c fom libevent is giving me a hard time.

libevent/evutil.c: error: implicit declaration of function 'pipe2' is invalid in C99

The code involved is:

    if (pipe2(fd, O_NONBLOCK|O_CLOEXEC) == 0)
        return 0;

I can't update my code to c11 right now. How should I change the code to not get this error anymore?

Carla Camargo
  • 574
  • 5
  • 14
  • 2
    The error message tells you that the function `pipe2` has not been declared. You have to find out the standard header that declares it and include it. – axiac Mar 27 '19 at 20:10
  • You are probably compiling in C90, where implicit declarations were allowed. You should probably find a header file with it in it? – Neil Mar 27 '19 at 20:10
  • 1
    Or you're compiling in strict (`-std=c99`) instead of gnu (`-std=gnu99`) mode, which disables a bunch of feature test macros. – Shawn Mar 27 '19 at 20:25
  • Possible duplicate of [warning: implicit declaration of function](https://stackoverflow.com/questions/8440816/warning-implicit-declaration-of-function) –  Mar 27 '19 at 20:38

4 Answers4

2

This isn't a C99 issue. You need to include the header for pipe2. According to the pipe2 manual that is unistd.h.

Why libevent isn't doing this itself is a valid question.

Schwern
  • 153,029
  • 25
  • 195
  • 336
2

You need to include headers which declare functions you are using. In order to figure out which headers you need, you need to consult function documentation. On Posix functions, the best source is man.

man pipe2 will give you following:

PIPE(2)                    Linux Programmer’s Manual                   PIPE(2)

NAME
       pipe, pipe2 - create pipe

SYNOPSIS
       #include <unistd.h>

       int pipe(int pipefd[2]);

       #define _GNU_SOURCE
       #include <unistd.h>

       int pipe2(int pipefd[2], int flags);

Right there, in the Synopsis, you will see required header files.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
0

At the top of the manpage is

#define _GNU_SOURCE             /* See feature_test_macros(7) */
#include <fcntl.h>              /* Obtain O_* constant definitions */
#include <unistd.h>

You need those headers and the feature test macro.

Then the identifier should become available, at least on Linux/glibc.

Example:

#define _GNU_SOURCE             /* See feature_test_macros(7) */
#include <fcntl.h>              /* Obtain O_* constant definitions */
#include <unistd.h>

int main()
{
    (void)&pipe;
}
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
0

Upgrading to C11 is not the answer; rather downgrading to C90 where implicit declarations are allowed (but will generate a warning), or at least compiling with more permissive compiler options - perhaps -std=gnu99 or -std=c90 in combination with -Wno-implicit to suppress the warning.

A better alternative is to include the appropriate header <unistd.h> in evutil.c, however you may prefer not to modify the library code, in which case you can compile it with a forced include with the compiler option -include unistd.h. This pre-processor option will process the source file file as if #include "file" appeared as the first line.

Clifford
  • 88,407
  • 13
  • 85
  • 165