Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ncurses basic example - in debug i get: "Error opening terminal: unknown."

doing some basic examples on ncurses libreries, I get some issues.

Actually, I don't get what I expect (message printed), and going in debug, from eclipse, I get (in console area) "Error opening terminal: unknown."

Follows code:

#include <unistd.h>
#include <stdlib.h>
#include <ncurses.h>


int main() {

    initscr();

    move(5,15);
    printw("%s", "Hello world!");
    refresh();

    endwin();
    exit(EXIT_SUCCESS);
}

Compiler options, as provided in Eclipse console at "Build project" command:

make all 
Building file: ../source/Curses_01.c
Invoking: GCC C Compiler
gcc -Incurses -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"source/Curses_01.d"     -MT"source/Curses_01.d" -o"source/Curses_01.o" "../source/Curses_01.c"
Finished building: ../source/Curses_01.c

Building target: Curses_01
Invoking: GCC C Linker
gcc  -o"Curses_01"  ./source/Curses_01.o   -lcurses
Finished building target: Curses_01

Thanks everybody in advance!

like image 233
graziano governatori Avatar asked Nov 02 '12 12:11

graziano governatori


1 Answers

You do get the string printed. The problem is that the program exits immediately. This will clear the screen and restore it to its previous state. This happens very fast, of course, so you don't get to see anything.

The solution is to wait for a keypress before exiting. You can do this with getch():

/* ... */
refresh();
getch();
endwin();
exit(EXIT_SUCCESS);

The Eclipse problem arises due to the terminal presented by Eclipse to the application. NCurses doesn't recognize it. I don't use Eclipse, so I don't know exactly how to do this, but you should search for some setting that allows you to run the application inside a full terminal (like in xterm, Konsole, Gnome Terminal, etc.)

like image 175
Nikos C. Avatar answered Oct 28 '22 00:10

Nikos C.