【问题标题】:F_GETPIPE_SZ undeclaredF_GETPIPE_SZ 未声明
【发布时间】:2021-11-21 15:15:58
【问题描述】:

我正在尝试获取管道的大小:

printf("pipe 0 size: %d bytes\npipe 1 size: %d bytes\n", fcntl(fd[0], F_GETPIPE_SZ), fcntl(fd[1], F_GETPIPE_SZ));

使用的标头(其中一半被另一部分代码使用):

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/wait.h>

当我尝试编译时,gcc 失败并出现以下错误:

‘F_GETPIPE_SZ’未声明(在此函数中首次使用)

内核版本 - 5.4.0-88-generic

libc6-dev 版本 - 2.31-0ubuntu9.2

gcc 版本 - 4:9.3.0-1ubuntu2

【问题讨论】:

  • 阅读手册页man fcntl,它提到你需要__GNU_SOURCE 的宏F_GETPIPE_SZ

标签: c linux


【解决方案1】:

由于此宏不是 POSIX 的一部分,因此您必须在包含 &lt;fcntl.h&gt; 之前定义 _GNU_SOURCE 功能测试宏。

这在the fcntl(2) man page 中的“符合”部分中进行了说明。

What does "#define _GNU_SOURCE" imply?

【讨论】:

    猜你喜欢
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 2010-12-19
    • 2011-02-06
    • 2013-05-07
    • 2021-08-04
    • 2019-03-21
    相关资源
    最近更新 更多