Page 1 of 1

about ncurses.h

Posted: Fri Mar 18, 2016 5:05 am
by Dekata
Guys, Im very new in Rasp and C, My program use ncurses.h but it seem cant use getch();

Code: Select all

#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <wiringPi.h>
#include <wiringSerial.h>
#include <ncurses.h>
#include <stdint.h>
int main() {
int key;
key = getch();
errors undefind refrence to wgetch and stdscrt . I dont know why?
any suggestion for me!!!!!!! thanks alot alot alot alot.

Re: about ncurses.h

Posted: Fri Mar 18, 2016 8:34 am
by DaveDriesen
How are you compiling this? Did you link to ncurses in your compile command?

You need to add -lncurses so that the compiler knows to link against ncurses..

Code: Select all

gcc -o test -lncurses test.c
or use a Makefile. Here's a recent Makefile for a simple (C++) curses app:

Code: Select all

CC              = g++
LIBS            = -lcurses
CFLAGS          = -fpermissive -g -O2 -Wall
PROGRAMS        = test

        $(CC) $(CFLAGS) $(LIBS) -o test test.cpp

        $(RM) *.o $(PROGRAMS) *~

        @cp $(PROGRAMS) /usr/local/bin
Note I'm linking to "curses" here, which usually symlinks to "ncurses" but I prefer to use this for compatibility reasons. Most of the time there is no practical difference.


Re: about ncurses.h

Posted: Fri Mar 18, 2016 8:36 am
by PeterO
Unlike Python, using a library in C is a two step process.

Step 1) Add the appropriate "#include" statments to your source code.
Step 2) add the appropriate library names to the compiler command line.

You have the correct "#include" statments but the errror message suggests you havn't told the compiler to link with the library.

Typically you add a "-l" switch followed by the library name (like this -lncurses) to the gcc command.

Code: Select all

pi@Pi3:~ $ gcc -o nc nc.c
/tmp/ccQptShA.o: In function `main':
nc.c:(.text+0x18): undefined reference to `wgetch'
nc.c:(.text+0x24): undefined reference to `stdscr'
collect2: error: ld returned 1 exit status
pi@Pi3:~ $ gcc -o nc nc.c -lncurses
pi@Pi3:~ $