Solved ncurses lib: test failed

I'm having a hard time implementing the ncurses library in FreeBSD 10

This is what I do:

1. install lib: pkg install ncurses-5.9.20150214
2. src file:

Code:
#include <ncurses.h>

int main(void) {
  initsrc();
  addstr("Hello World!");
  refresh();
  getch();

  endwin();

  return 0;
}

3. compile: cc -o ncurses.o ncurses.c (do I need to link any lib. here?)

4. output:
Code:
ncurses.c:7:3: warning: implicit declaration of function 'initsrc' is invalid in
      C99 [-Wimplicit-function-declaration]
  initsrc();
  ^
1 warning generated.
/tmp/ncurses-xTwg1E.o: In function `main':
ncurses.c:(.text+0x19): undefined reference to `initsrc'
ncurses.c:(.text+0x26): undefined reference to `curs_set'
ncurses.c:(.text+0x3b): undefined reference to `stdscr'
ncurses.c:(.text+0x43): undefined reference to `waddnstr'
ncurses.c:(.text+0x4b): undefined reference to `stdscr'
ncurses.c:(.text+0x53): undefined reference to `wrefresh'
ncurses.c:(.text+0x5b): undefined reference to `stdscr'
ncurses.c:(.text+0x63): undefined reference to `wgetch'
ncurses.c:(.text+0x6b): undefined reference to `endwin'
cc: error: linker command failed with exit code 1 (use -v to see invocation)

*At some point I tried using both, the
Code:
#include <ncurses.h>
and
Code:
#include <curses.h>
with the same result.

What am I missing?
 
You need to link with ncurses. Add -lncurses to your compile command. Note that this will you use the base's ncurses library not the one you installed with pkg.

You also have a typo: initsrc should be initscr.

HTH
 
That did it. Thanks so much. Now, I wanted to ask ... how would I use the pkg one? In my mind pkg would replace the system's lib. I'm thinking about further updates performed from the pkg tool (to this library) vs using the (supposedly) older base version.
 
Add the output of pkg-config --cflags --libs ncurses tinfo to the compile command. You'd also need to install devel/pkgconf for this.

cc `pkg-config --cflags --libs ncurses tinfo` -o ncurses ncurses.c

You can double check with ldd(1) that it links with the ports version in /usr/local/lib: ldd ncurses
It should output something like:
Code:
ncurses:
	libncurses.so.5 => /usr/local/lib/libncurses.so.5 (0x80081f000)
	libtinfo.so.5 => /usr/local/lib/libtinfo.so.5 (0x800a40000)
	libc.so.7 => /lib/libc.so.7 (0x800c75000)
 
pkg-config will work on Linux too. It has nothing to do with FreeBSD's pkg even though the names are similar.
 
Back
Top