k.g
k.g

Reputation: 13

program wont compile via Makefile when using ncurses library?

I'm trying to make a program using the ncurses library, but for some reason when I try to compile using my Makefile it says there's an error in the Makefile and all of the functions from ncurses are undefined. I included the library in the main.c file correctly since when I compile using gcc main.c -lncurses the program works correctly and doesn't give any errors or say that anything is undefined. I've tried messing around with the Makefile and I can't get it to work properly, does anyone have a solution?

Makefile:

CC = gcc
CFLAGS = -Wall --std=c99 -g
LDFLAGS = -lncurses
OBJECTS = main.o
ALL: space_invaders
space_invaders: $(OBJECTS)
        $(CC) $(CFLAGS) -o space_invaders $(OBJECTS)
main.o: main.c
        $(CC) $(CFLAGS) -c main.c -o main.o
clean:
        rm space_invaders $(OBJECTS)

Output:

gcc -Wall --std=c99 -g -o space_invaders main.o /usr/bin/ld: main.o: in function main': /home/kyle/space_invaders/main.c:9: undefined reference to initscr' /usr/bin/ld: /home/kyle/space_invaders/main.c:10: undefined reference to noecho' /usr/bin/ld: /home/kyle/space_invaders/main.c:11: undefined reference to curs_set' /usr/bin/ld: /home/kyle/space_invaders/main.c:22: undefined reference to stdscr' /usr/bin/ld: /home/kyle/space_invaders/main.c:22: undefined reference to stdscr' /usr/bin/ld: /home/kyle/space_invaders/main.c:22: undefined reference to stdscr' /usr/bin/ld: /home/kyle/space_invaders/main.c:22: undefined reference to stdscr' /usr/bin/ld: /home/kyle/space_invaders/main.c:23: undefined reference to stdscr' /usr/bin/ld: /home/kyle/space_invaders/main.c:23: undefined reference to wclear' /usr/bin/ld: /home/kyle/space_invaders/main.c:24: undefined reference to mvprintw' /usr/bin/ld: /home/kyle/space_invaders/main.c:25: undefined reference to stdscr' /usr/bin/ld: /home/kyle/space_invaders/main.c:25: undefined reference to `wrefresh' collect2: error: ld returned 1 exit status make: *** [Makefile:7: space_invaders] Error 1

Upvotes: 0

Views: 593

Answers (2)

Luis Colorado
Luis Colorado

Reputation: 12708

you have to specify the $(LDFLAGS) variable in the linking command:

space_invaders: $(OBJECTS)
        $(CC) $(CFLAGS) $(LDFLAGS) -o space_invaders $(OBJECTS)

As you probably have seen, the linking command in the output of your run doesn't show the -lncurses option, so the library has not been including, making all the calls to library functions to be unresolved.

I don't recommend you to use the alternative given in another answer about using the implicit linking rule, as it is applicable only to GNU make, and so, it is not portable to other make's around.

Upvotes: 0

kaylum
kaylum

Reputation: 14044

LDFLAGS is defined but not used. Add it to the rule that links the final executable.

space_invaders: $(OBJECTS)
    $(CC) $(CFLAGS) -o space_invaders $(OBJECTS) $(LDFLAGS)

Alternatively the Make implicit rules can be used to simplify the makefile:

CC = gcc
CFLAGS = -Wall --std=c99 -g
LDFLAGS = -lncurses
OBJECTS = main.o
ALL: space_invaders

space_invaders: $(OBJECTS)

clean:
        rm space_invaders $(OBJECTS)

Upvotes: 1

Related Questions