Search code examples
c++gcclinker-errorsncurses

g++ can't find ncurses.h despite it being installed


I'm trying to experiment around in ncurses for the first time, but I'm having problems compiling my source code. As far as I can tell, ncurses is installed and in the proper directories.

My makefile is super simple:

.cpp :
    g++ -Wall -g -o $* $*.cpp -std=c++11 -lncurses

and here's my output when I try to locate ncurses.h

$ locate ncurses.h
/usr/include/ncursesw/ncurses.h

and when I check to see if it's installed

$ dpkg -l | grep ncurses
ii  libncurses5:amd64                                     5.9+20140118-1ubuntu1                               amd64        shared libraries for terminal handling
ii  libncursesw5:amd64                                    5.9+20140118-1ubuntu1                               amd64        shared libraries for terminal handling (wide character support)
ii  libncursesw5-dev:amd64                                5.9+20140118-1ubuntu1                               amd64        developer's libraries for ncursesw
ii  mtr-tiny                                              0.85-2                                              amd64        Full screen ncurses traceroute tool
ii  ncurses-base                                          5.9+20140118-1ubuntu1                               all          basic terminal type definitions
ii  ncurses-bin                                           5.9+20140118-1ubuntu1                               amd64        terminal-related programs and man pages
ii  ncurses-term                                          5.9+20140118-1ubuntu1                               all          additional terminal type definitions

But g++ tells me this when I try to make

bankacct.cpp:18:29: fatal error: ncurses.h: No such file or directory
compilation terminated.

Unfortunately, I've not got root access and I need to be able to compile on this machine. What are my options?


I've tried including <ncursesw/ncurses.h> based on suggestions from other users, but now g++ is giving me this error:

$ make bankacct
g++ -Wall -g -o bankacct bankacct.cpp -std=c++11 -lncurses
/usr/bin/ld: cannot find -lncurses

and if I try removing -lncurses it gives me this:

$ make bankacct
g++ -Wall -g -o bankacct bankacct.cpp -std=c++11
/tmp/cc8rPQfK.o: In function `main':
bankacct.cpp:23: undefined reference to `initscr'

Now I've tried linking the libraries. Here's what I did:

$ locate libncurse
/lib/x86_64-linux-gnu/libncurses.so.5
/lib/x86_64-linux-gnu/libncurses.so.5.9
/lib/x86_64-linux-gnu/libncursesw.so.5
/lib/x86_64-linux-gnu/libncursesw.so.5.9
/usr/lib/x86_64-linux-gnu/libncurses++w.a
/usr/lib/x86_64-linux-gnu/libncursesw.a
/usr/lib/x86_64-linux-gnu/libncursesw.so
/usr/share/doc/libncurses5
/usr/share/doc/libncursesw5
/usr/share/doc/libncursesw5-dev
/var/lib/dpkg/info/libncurses5:amd64.list
/var/lib/dpkg/info/libncurses5:amd64.md5sums
/var/lib/dpkg/info/libncurses5:amd64.postinst
/var/lib/dpkg/info/libncurses5:amd64.postrm
/var/lib/dpkg/info/libncurses5:amd64.shlibs
/var/lib/dpkg/info/libncurses5:amd64.symbols
/var/lib/dpkg/info/libncursesw5-dev:amd64.list
/var/lib/dpkg/info/libncursesw5-dev:amd64.md5sums
/var/lib/dpkg/info/libncursesw5-dev:amd64.postinst
/var/lib/dpkg/info/libncursesw5:amd64.list
/var/lib/dpkg/info/libncursesw5:amd64.md5sums
/var/lib/dpkg/info/libncursesw5:amd64.postinst
/var/lib/dpkg/info/libncursesw5:amd64.postrm
/var/lib/dpkg/info/libncursesw5:amd64.shlibs
/var/lib/dpkg/info/libncursesw5:amd64.symbols

So then I tried two variations of my makefile:

g++ -Wall -g -L/usr/lib/x86_64-linux-gnu/ -o $* $*.cpp -std=c++11 -lncurses

and

g++ -Wall -g -L/lib/x86_64-linux-gnu/ -o $* $*.cpp -std=c++11 -lncurses

which still gave me the errors undefined reference to 'initscr' (without -lncurses) or /usr/bin/ld: cannot find -lncurses (with it)


Solution

  • -lncurses
    

    tells the linker to look for a library called "ncurses.". You clearly indicate that's not what your library is called:

    /usr/lib/x86_64-linux-gnu/libncursesw.a
    

    You need

    -lncursesw
    

    You don't need to modify the source code to specify <ncursesw/ncurses.h> you can simply add

    -I/usr/include/ncursesw