Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined reference to functions in libncurses

Tags:

c

linux

gcc

ncurses

When I compile this code, I get the following gcc errors:

/tmp/ccUigsI6.o: In function `main':
/home/matt/Dropbox/school/2011/cs3210/test/sizeterm.c:9: undefined reference to `setupterm'
/home/matt/Dropbox/school/2011/cs3210/test/sizeterm.c:10: undefined reference to `tigetnum'
/home/matt/Dropbox/school/2011/cs3210/test/sizeterm.c:11: undefined reference to `tigetnum'
collect2: ld returned 1 exit status
make: *** [sizeterm] Error 1

Here's the code:

#include <stdio.h>
#include <term.h>
#include <curses.h>
#include <stdlib.h>

int main()
{
    int nrows, ncolumns;
    setupterm(NULL, fileno(stdout), (int *)0);
    nrows = tigetnum("lines");
    ncolumns = tigetnum("cols");
    printf("This terminal has %d columns and %d rows\n", ncolumns, nrows);
    exit(0);
}

Libncurses is installed correctly on my machine. I get the same results from my Arch linux laptop, and the Ubuntu server installed at my school. This particular piece of code is taken directly out of the book. Am I doing something wrong? I've done some googling and it looks as though people have had this problem before, but I can't narrow down a solution.

like image 905
Matt Avatar asked Jan 01 '26 11:01

Matt


2 Answers

You forgot to actually link against ncurses. Add -lcurses to the gcc command line.

like image 131
Ignacio Vazquez-Abrams Avatar answered Jan 03 '26 02:01

Ignacio Vazquez-Abrams


This is exactly what you find in the same book as where you found this code:

$ cc -o badterm badterm.c -lncurses

Beginning linux programming 4th edition, chapter 5: Terminals, page 196.

like image 36
alex Avatar answered Jan 03 '26 03:01

alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!