Search code examples
cimplicit-declaration

cfmakeraw implicit declaration error


I am writing multithread application (as homework). One of those threads is dedicated for reading keyboard presses, so using terminal in raw mode. But I keep getting error

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

even though I have unistd.h and termios.h included.

I am programming on Linux (xubuntu 16.04) using gcc 5.4.0 with -std=c99 flag. The code looks something like:

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

Solution

  • As the manpage says,

    Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

    cfsetspeed(), cfmakeraw():
    Since glibc 2.19:
    _DEFAULT_SOURCE
    Glibc 2.19 and earlier:
    _BSD_SOURCE

    So you should either add -D_BSD_SOURCE -D_DEFAULT_SOURCE to the command-line, or add #define _BSD_SOURCE and #define _DEFAULT_SOURCE before any system #include.