cannot compile with ncurses

Asked by vergarr

I am exploring the ncurses library functions so that I can use them in my own programs. I copied the sample code from
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/helloworld.html
to make a simple "HelloWorld" program, but when I try to compile I get the following errors:

/tmp/cctGezOm.o: In function `main':
helloworld.c:(.text+0x12): undefined reference to `initscr'
helloworld.c:(.text+0x1e): undefined reference to `printw'
helloworld.c:(.text+0x23): undefined reference to `stdscr'
helloworld.c:(.text+0x2b): undefined reference to `wrefresh'
helloworld.c:(.text+0x30): undefined reference to `stdscr'
helloworld.c:(.text+0x38): undefined reference to `wgetch'
helloworld.c:(.text+0x3d): undefined reference to `endwin'
collect2: ld returned 1 exit status

I checked synaptic to be sure I had the headers installed. I have libncurses5, libncurses5-dev, libncursesw5, ncurses-base, & ncurse-bin. Searching for ncurses.h finds it @ /usr/include so I'm stumped as to why the program wont compile.

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu ncurses Edit question
Assignee:
No assignee Edit question
Solved by:
W. Prins
Solved:
Last query:
Last reply:
Revision history for this message
vergarr (moonsdad) said :
#1

I just noticed that my math functions are also not compiling. math.h is also in /usr/include

Revision history for this message
W. Prins (wprins) said :
#2

How are you compiling your program? It sounds like you're not telling the compiler to link in the ncurses (or math) libraries when you compile, hence the undefined reference errors. Post the command you're using to compile.

Revision history for this message
vergarr (moonsdad) said :
#3

Thanks for the reply. The command I'm using in both instances is:
gcc filename.c

Revision history for this message
vergarr (moonsdad) said :
#4

I checked the man page for gcc to see what it had to say about linking libraries while compiling. Adding -lncurses as an argument to gcc worked. But -lmath didn't work for the math.h library.

/usr/bin/ld: cannot find -lmath
collect2: ld returned 1 exit status

Revision history for this message
Best W. Prins (wprins) said :
#5

OK, the option for linking in the math library is just "-lm" not "-lmath" since the library is called "libm" not "libmath", even if the C include file that describes it's C interface is called "math.h"

In general if you need to link a library, the flag will follow the form -l<name of library>, for example, to link libpthread use -lpthread. Libraries are normally kept under /lib and /usr/lib. Have a peek to see what's there.

Revision history for this message
vergarr (moonsdad) said :
#6

Thanks ByteJuggler, that solved my question.

Revision history for this message
Jacob MacDonald (jaccarmac) said :
#7

I'm using the same gcc flags and I'm still getting the undefined reference errors.

Revision history for this message
vergarr (moonsdad) said :
#8

Did you make sure that you have the libraries installed?

Revision history for this message
Jacob MacDonald (jaccarmac) said :
#9

On Ubuntu 11.10, libncurses5, libncurses5-dev, libncursesw5, and libncursesw5 are installed. An interesting thing is that the compiler seems to be linking curses: that is, when I use the -lncurses5 flag it cannot find that library (I don't get an error for -lncurses other than it won't link the functions) but the compiler cannot seem to find the functions in the library.

Revision history for this message
Jacob MacDonald (jaccarmac) said :
#10

Update: linking manually with some libraries from /lib worked. However, I'd still like to know how *not* to do it that way.

Revision history for this message
Jacob MacDonald (jaccarmac) said :
#11

Problem is solved after looking through Cygwin email discussions. For anyone having similar problems: -lx arguments, where x is your library, should always follow the source and object files. For consistency, I'm moving all arguments to the end of the make line, so "gcc -o runme -lncurses runme.c" turns into "gcc runme.c -o runme -lncurses".