Ncurses.h function problems

Oct 18, 2019 at 4:12am
I was looking at the ncurses.h header today because I discovered I had it already, and I just basically copied someone else’s code for how to make a window (just initscr) and it says the function doesn’t exist or something like that. It says 'undefined reference to ‘initscr’'

1
2
3
4
5
6
7
8
#include <ncurses.h>

int main(){
  WINDOW * initscr();
  printw("Hello World");
  refresh();
  return 0;
}


Edit: sry it was curses.h not ncurses, same problem though.
Last edited on Oct 18, 2019 at 4:12am
Oct 18, 2019 at 4:39am
Well you also have to link with the ncurses library as well.

gcc prog.c -lncurses
Oct 18, 2019 at 5:17am
Many systems deviate a little from the correct way...

The header is <curses.h>. There should not be systems now with headers ancient enough to have a difference between <curses.h> and <ncurses.h> — to the point that one should just be a hardlink to the other, assuming <ncurses.h> exists. SunOS tends to be weird, so if you are using SunOS you might have an unusual include path.

Likewise, as salem c indicated, you need to link with the library.
Again, you may have to use pkg-config to find where it is, but if you used your package manager to install the ncurses library, it should be in one of the default search locations.

BTW, your code is incorrect: you are declaring the initscr() function, not calling it. Make sure to call it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <curses.h>
int main()
{
  initscr();
  // There are probably other things you would like to initialize here as well

  // display output
  printw( "Hello world!" );
  refresh();

  // don't terminate until a key is pressed
  nodelay( stdscr, FALSE );
  getch();
}

It is worth your time to Google the NCURSES-Programming-HOWTO.

Good luck!
Oct 18, 2019 at 12:35pm
Thanks guys! That worked, I was just linking it wrong.

@Duthomas yeah that was a typo sorry about that, it’s supposed to be WINDOW * wnd = initscr();
Topic archived. No new replies allowed.