0

I noticed my program doesn't build on another distro. I have -lncurses on makefile CFLAGS but get "undefined reference to `initscr'" errors.

Here is the makefile:

CFLAGS+=-std=c99 -pedantic -Wall -lncurses
BIN=progname

all: $(BIN)

install: all
    mkdir -p $(DESTDIR)/usr/bin
    install -m 755 $(BIN) $(DESTDIR)/usr/bin/

uninstall:
    rm -f $(DESTDIR)/usr/bin/$(BIN)

clean:
    rm -f $(BIN)

Here is the cc command:

cc -std=c99 -pedantic -Wall -lncurses    nbwmon.c   -o nbwmon

If i move -lncurses last it builds fine:

cc -std=c99 -pedantic -Wall    nbwmon.c   -o nbwmon -lncurses

So how can i fix this? How can i move -lncurses directive last on makefile?

Ari Malinen
  • 576
  • 2
  • 6
  • 20
  • I don't think this is a duplicate, the question is how to reorganize the makefile rather than why the gcc triggers "undefined reference" – Piotr Skotnicki Aug 24 '14 at 09:55

2 Answers2

1

It's because the linker looks up dependencies in a kind of reverse order, so if object file O depends on library L, then the library L must be after the object file O in the command line.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

Move -lncurses from CFLAGS into LDFLAGS:

CFLAGS+=-std=c99 -pedantic -Wall
LDFLAGS+=-lncurses
Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160