tags:

views:

319

answers:

2

I'm trying to start developing a program using ncurses on Linux. I can't even get the Hello World example to compile. Here's the code:

#include <curses.h>
int
main()
{         
        initscr();
        printw("Hello, world.");
        refresh();
        getch();
        endwin();
        return 0;
}

When I attempt to compile, I get:

hello.c:(.text+0x12): undefined reference to `iniscr'

For every one of the those caled functions.

I installed ncurses via apt-get, and also by downloading the sources and compiling, installing, etc.

I have tried #include both curses.h and ncurses.h.

What is going on?

+3  A: 

Have you used the -lcurses option when linking?

Including the header files let the code compile (because the compiler knows what the function call looks like from the .h file), but the linker needs the library file to find the actual code to link into your program.

Greg Hewgill
+2  A: 

As Greg Hewgill said, you need to pass in -lcurses or -lncurses to link to the curses library.

gcc -lncurses -o hello hello.c

You also probably mean to use initscr() and getch(). Once I make those substitutions, the above compiles for me.

Brian Campbell