繁体   English   中英

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

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

我正在尝试构建我的库,但是一个名为evutil.c fom libevent的文件让我很难过。

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

涉及的代码是:

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

我现在无法将代码更新到c11。 我应该如何更改代码以不再出现此错误?

这不是C99问题。 您需要包含pipe2的标头。 根据pipe2手册unistd.h

为什么libevent不这样做本身就是一个有效的问题。

您需要包含声明您正在使用的函数的标头。 为了确定您需要哪些标头,您需要查阅功能文档。 在Posix函数上,最好的来源是man

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);

就在那里,在概要中,您将看到所需的头文件。

联机帮助页的顶部是

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

您需要这些标头和功能测试宏。

然后标识符应该可用,至少在Linux / glibc上。

例:

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

int main()
{
    (void)&pipe;
}

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

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

暂无
暂无

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

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