0
votes

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

2
Please do not post code and text output as images - reasoning. Copy it as formatted text into the question.kaylum
The makefile defines LDFLAGS but never uses it.kaylum
@kaylum where in the makefile would I use it? I tried looking it up and couldn't figure out where it would be used.k.g
Well, if you build it manually where do you add that flag? Put it in the place in the Makfile that corresponds to that same command. That is, add it to command that links the final executable.kaylum
@kaylum Thanks, I put it there and it works properly now.k.g

2 Answers

1
votes

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)
0
votes

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.