Posts: 1
Joined: Thu Mar 17, 2016 8:47 am

about ncurses.h

Fri Mar 18, 2016 5:05 am

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.

User avatar
Posts: 113
Joined: Sun Mar 31, 2013 8:28 pm
Location: Top of the food chain
Contact: Website

Re: about ncurses.h

Fri Mar 18, 2016 8:34 am

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.


User avatar
Posts: 6095
Joined: Sun Jul 22, 2012 4:14 pm

Re: about ncurses.h

Fri Mar 18, 2016 8:36 am

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:~ $ 
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Return to “C/C++”