Ubuntu – How to compile programs that use term.h

14.04ccompilinggcc

I try to compile the code below:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <term.h>

//#include "/usr/include/term.h"

void clear_screen(void) {
    if (!cur_term) {
        int result;
        setupterm( NULL, STDOUT_FILENO, &result );
    if (result <= 0)
        return;
    }

    putp( tigetstr( "clear" ) );
}

int main(void) {
    puts("I am going to clear the screen");
    sleep(1);
    clear_screen();
    puts("Screen Cleared");
    sleep(1);
    clear_screen();

    return 0;

}

This program tests a function that uses the term.h header file to clear the terminal screen.
I have installed all the libraries needed: libncurses5 and libncurses5-dev and when I compile the program with or without the -lncurses, -lcurses and -lterminfo parameters to the gcc compiler I get the above output:

In file included from clear_screen_UNIX.c:5:0:
clear_screen_UNIX.c:9:6: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token
 void clear_screen(void) {
      ^
clear_screen_UNIX.c: In function ‘main’:
clear_screen_UNIX.c:23:14: error: called object is not a function or function pointer
  clear_screen();
              ^
clear_screen_UNIX.c:26:14: error: called object is not a function or function pointer
  clear_screen();
              ^

Moreover, when I explicitly include the term.h header using its full path /usr/include/term.h I get this as the output:

In file included from clear_screen_UNIX.c:7:0:
/usr/include/term.h:125:21: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token
 #define CUR cur_term->type.
                     ^
/usr/include/term.h:202:40: note: in expansion of macro ‘CUR’
 #define clear_screen                   CUR Strings[5]
                                        ^
clear_screen_UNIX.c:9:6: note: in expansion of macro ‘clear_screen’
 void clear_screen(void) {
      ^
clear_screen_UNIX.c: In function ‘main’:
clear_screen_UNIX.c:23:14: error: called object is not a function or function pointer
  clear_screen();
              ^
clear_screen_UNIX.c:26:14: error: called object is not a function or function pointer
  clear_screen();
              ^

According to the above, the term.h header has some mistakes inside, something I do not believe.

So do I need to install some more packages or configure some settings for the code to compile successfully?

PS:

  • The code example is from here
  • My Ubuntu version is 14.04

Best Answer

Indeed, clear_screen is a macro:

$ grep clear_screen /usr/include/term.h
#define clear_screen                   CUR Strings[5]

You will have to use some other name, like the linked page does:

void Clear_screen (void)

The compiler messages also state this:

clear_screen_UNIX.c:9:6: note: in expansion of macro ‘clear_screen’

That's why the errors seem to be in term.h - the macro expansion leads there.