17

I've just discovered ncurses and have just started learning it, however the examples in my tutorial don't compile on my computer.

I had to install ncurses manually and did so by entering "apt-get install libncurses5-dev libncursesw5-dev". I had to do this because before having done so I got an error saying that I could not "#include ".

Installing it worked, but now I get this error instead:

touzen@comp:~/learning_ncurses$ g++ -o hello_world hello_world.cpp
/tmp/ccubZbvK.o: In function `main':
hello_world.cpp:(.text+0xa): undefined reference to `initscr'
hello_world.cpp:(.text+0x16): undefined reference to `printw'
hello_world.cpp:(.text+0x1b): undefined reference to `refresh'
hello_world.cpp:(.text+0x20): undefined reference to `stdscr'
hello_world.cpp:(.text+0x28): undefined reference to `wgetch'
hello_world.cpp:(.text+0x2d): undefined reference to `endwin'
collect2: ld returned 1 exit status

The code I compiled looks like this:

#include <ncurses.h>
int main(){
    initscr();
    printw("Hai thar world...");
    refresh();
    getch();
    endwin();

    return 0;
}

Why do I get this error. And even more importantly, how do I fix this?

thomasvakili
  • 1,138
  • 2
  • 10
  • 17

1 Answers1

39

you have to link the ncurses library

g++ -o hello_world hello_world.cpp -lncurses
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • 1
    I'm quite new to C++ so this concept is new to me. Do I always have to do this whenever I'm using a library not included in the compiler? – thomasvakili Jun 30 '11 at 16:57
  • 3
    Yes. The header file allows your code to compile by matching a function declaration to calls in your code. The linker's job is to find the *actual runnable code* for every function used in your program, and that's by specifying the encapsulating library name at link-time (as well as the object files for your own code). – Steve Townsend Jun 30 '11 at 16:59
  • Exactly - you need to link to all libraries, even the standard ones. – Paul Beckingham Jun 30 '11 at 17:00
  • 1
    The only reason you haven't realized this so far is that GCC automatically links `libc` (unless you tell it not to do so with -ffreestanding) – Karoly Horvath Jun 30 '11 at 17:09
  • it is the common behaviour in other compilers too – ShinTakezou Jun 30 '11 at 17:12
  • Just a note for cmake users and kdevelop 4.2 users with cmake: In cmakelists.txt add this line: `target_link_libraries(executablename "-lncurses")` and it will compile `executablename` with `-lncurses` flag. – Logan Mar 07 '12 at 18:42
  • Linker flags should follow sources and object files, see comment #11 [here](https://answers.launchpad.net/ubuntu/+source/ncurses/+question/51319). – mmoya Apr 16 '12 at 18:06
  • 2
    Had exactly same problem as OP but a bit different: I was following very basic hello world tutorial for ncurses and the tutorial explained how to compile, like this: `g++ -lncurses helloworld.cpp` which give same error as OP. So my problem was I needed the explanation that `-lncurses` **has to be at the end of the compiling command.** – aesede Jul 27 '14 at 04:02