2

I'm trying to get a character from the keyboard using curses.h. This is my source (get_char_example.c):

#include <curses.h>
#include <stdio.h>

int main(void)
{
    char ch;

    printf("Enter a character: ");
    ch = getch();
    printf("\nIts ASCII code is %d\n", ch);

    return 0;
}

I use the following command to compile:

gcc get_char_example.c -o get_char_example

And I get the following error:

/tmp/ccjaXwRU.o: In function `main':

get_char_example.c:(.text+0x1c): undefined reference to `stdscr'
get_char_example.c:(.text+0x24): undefined reference to `wgetch'
collect2: error: ld returned 1 exit status

using: gcc (GCC) 7.2.0 Arch Linux 4.13.9-1-ARCH

Thanks in advance!

1 Answers1

3

It is a linker error (see this, nearly a duplicate). ncurses is generally known to pkg-config (which gives more dependencies, when required), so try to compile with:

 gcc -Wall -Wextra -g \
    $(pkg-config --cflags ncurses) \
    get_char_example.c \
    $(pkg-config --libs ncurses)

You want to Invoke GCC with all warnings and debug info; you would use GDB, perhaps under emacs or with ddd, for debugging.

Even better, use some build automation tool (like make or ninja). With GNU make take inspiration from this to write your Makefile (where tab-s are significant).

On my Debian Sid, pkg-config --cflags ncurses gives -D_GNU_SOURCE -D_DEFAULT_SOURCE and pkg-config --libs ncurses gives -lncurses -ltinfo, so simply linking with -lncurses is not enough (and that is why this is not exactly a duplicate today in November 2017; a few years ago linking with simply -lncurses was enough, today it is not).

Use pkg-config --list-all to find all the packages and libraries known to pkg-config; when developing your libraries consider also providing some .pc file for it.

BTW, your program should start by initializing with initscr and use printw; Read Ncurses-programming-HowTo even if it is a bit old.

Notice also that using ASCII in the 21st century is obsolete. Programs and computers use UTF-8 everywhere, and this has profound consequences you should be aware of (since an UTF-8 character can span several bytes). My French keyboard has keys for ², §, and é (even with American keyboards your could paste them) and none of these are in ASCII. Consider also using some UTF-8 library like libunistring.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 1
    Why is that not a duplicate? They are clearly not linking with the library as covered by the answer in that duplicate – Chris Turner Nov 01 '17 at 17:29
  • 1
    Because the other answer don't mention `pkg-config` and on some distributions, notably my Debian Sid, `ncurses` depend on other libraries. – Basile Starynkevitch Nov 01 '17 at 17:30
  • Find a better duplicate then, rather than trying to score some quick and easy points from a common question – Chris Turner Nov 01 '17 at 17:35
  • 1
    I leave you to find one. I tried quickly to find a duplicate, and failed. – Basile Starynkevitch Nov 01 '17 at 17:36
  • But the one you found is not correct today in 2017 on Debian/Sid. It was correct a few years ago. `ncurses` depend now on `tinfo` and that was not the case in distributions a few years ago. Teaching newbies to use `pkg-config` when relevant is worthwhile, the other answer don't even mention it. – Basile Starynkevitch Nov 01 '17 at 17:39
  • @Basile Starynkevitch: So it compiles using your advice. But when I run it I get: `Enter a character: Its ASCII code is -1` But I do not touch the keyboard. It runs automatically. Obviously the further debugging is a problem ob my source. But the command works! –  Nov 01 '17 at 17:50