1

我正在编写多线程应用程序(作为作业)。其中一个线程专用于读取键盘按键,因此在原始模式下使用终端。但我不断收到错误

error: implicit declaration of function ‘cfmakeraw’ [-Werror=implicit-function-declaration]
     cfmakeraw(&tio);

即使我有unistd.htermios.h包括在内。

我正在使用带有 -std=c99 标志的 gcc 5.4.0 在 Linux (xubuntu 16.04) 上编程。代码看起来像:

#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <termios.h>
#include <pthread.h>

#include "prg_serial_nonblock.h"

void set_raw(_Bool set);

int main(int argc, char *argv[]) {
    // terminal raw mode
    set_raw(true);

    // ... some thread magic ...

    set_raw(false);
    printf("\n");
}

void set_raw(_Bool set) {
    static struct termios tio, tioOld;
    tcgetattr(STDIN_FILENO, &tio);

    if (set) { // put the terminal to raw
        tioOld = tio; //backup
        cfmakeraw(&tio);
        tio.c_lflag &= ~ECHO; // assure echo is disabled
        tcsetattr(STDIN_FILENO, TCSANOW, &tio);
    }
    else {      // set the previous settingsreset
        tcsetattr(STDIN_FILENO, TCSANOW, &tioOld);
    }
}
4

1 回答 1

2

正如手册页所说,

glibc 的功能测试宏要求(参见feature_test_macros(7)):

cfsetspeed (), cfmakeraw ():
自 glibc 2.19:
_DEFAULT_SOURCE
Glibc 2.19 及更早版本:
_BSD_SOURCE

因此,您应该添加-D_BSD_SOURCE -D_DEFAULT_SOURCE到命令行,或者在任何 system 之前添加#define _BSD_SOURCEand 。#define _DEFAULT_SOURCE#include

于 2017-04-27T16:39:08.180 回答