我正在编写多线程应用程序(作为作业)。其中一个线程专用于读取键盘按键,因此在原始模式下使用终端。但我不断收到错误
error: implicit declaration of function ‘cfmakeraw’ [-Werror=implicit-function-declaration]
cfmakeraw(&tio);
即使我有unistd.h和termios.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);
}
}