簡體   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