Message Board -  Archive

[ Login ] [ Create Account ]
[ Board List ] [ View Board ] [ Post Reply ]
  Author  Subject: Re: Linux C libary, using getch() in curses.h

Posted on 12-05-2001 02:07 a.m. ET  reply

Original Poster: "Mark W. Krentel" <>

> I'm trying to access the keyboard without printing to the screen. Jamsa's
> C/C++ Bible uses the conio.h file to use getch; however, I am using a Linux
> OS. I found the curses.h and ncurses.h files to have the same getch()
> function, but when I run my test.c program, I get errors.

The way to turn off the terminal echo is to call tcgetattr(3) to get a
termios struct, mask out ECHO in c_cflag, and then call tcsetattr(3).
Then read the characters and turn echo back on (or better, restore the
original termios struct). "Advanced Programming in the Unix Environment"
(Stevens) shows how to do this.

And why are you using getch(3) and curses? Curses is for moving the
cursor around the screen, like what vi(1) does. Unless you need a
curses style program (if you're unsure, then you don't), you should
use getc(3) or getchar(3) or scanf(3). I've never heard of conio.h
and I suspect that Jamsa's getch is their own concoction and not
related to curses's getch(3).

And if you're doing this to read a password, there's a libc function
for that: getpass(3), which will do the tcgetattr/tcsetattr for you.
(I don't understand why the man page says it's obsolete, they don't
suggest a replacement.)

> test.c
> CODE: char c = getch();
> ERROR: undefined reference to `stdscr'
> ERROR: undefined reference to `wgetch'

Whenever you use a library function in C, you need to do two things.
First, include the appropriate header files in your .c file. For
wgetch(3), you would use:

#include <curses.h>

Second, tell gcc what library to use. The library naming rules in
Unix are cryptic and arcane. You tell gcc "-lfoo" for library wgetch(3) is in the ncurses library (
on my RH 7.1 system), so you would invoke gcc with something like:

gcc main.c -lncurses

And how do you find the correct header files for each library function?
You read its man page, so actually that's three things to do.

But in this case, I suspect you probably shouldn't be using curses and
wgetch. After you replace them with getchar, I'll bet most of these
errors will go away.


< Previous 1 Next >

Site Contents