简体   繁体   English

如何修复'隐含的函数声明'pipe2'在C99中无效'

[英]How to fix 'implicit declaration of function 'pipe2' is invalid in C99'

I'm trying to build my library but a file called evutil.c fom libevent is giving me a hard time. 我正在尝试构建我的库,但是一个名为evutil.c fom libevent的文件让我很难过。

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. 我现在无法将代码更新到c11。 How should I change the code to not get this error anymore? 我应该如何更改代码以不再出现此错误?

This isn't a C99 issue. 这不是C99问题。 You need to include the header for pipe2 . 您需要包含pipe2的标头。 According to the pipe2 manual that is unistd.h . 根据pipe2手册unistd.h

Why libevent isn't doing this itself is a valid question. 为什么libevent不这样做本身就是一个有效的问题。

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 . 在Posix函数上,最好的来源是man

man pipe2 will give you following: man pipe2会给你以下:

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. 就在那里,在概要中,您将看到所需的头文件。

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. 然后标识符应该可用,至少在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;
}

Upgrading to C11 is not the answer; 升级到C11不是答案; 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. 而是降级到C90,其中允许隐式声明(但会生成警告),或者至少使用更宽松的编译器选项进行编译 - 可能-std=gnu99-std=c90-Wno-implicit结合使用以抑制警告。

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 . 一个更好的选择是在evutil.c中包含适当的头<unistd.h> ,但是你可能不想修改库代码,在这种情况下你可以使用带有编译器选项的强制include来编译它 - 包括-include unistd.h This pre-processor option will process the source file file as if #include "file" appeared as the first line. 此预处理器选项将处理源文件文件,就好像#include“file”作为第一行出现一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM