0

I'm having some issues with a Makefile for a project I am working on. I am getting "undefined reference to 'timer_create'" and such even though they are included in the linkopts. I think the issue is that the libraries are at the front of the compile line instead at the end, but I am pretty unfamiliar with a Makefile like this. How can I ensure the links come at the end instead of at the beginning? Here is part of the Makefile I am talking about, the make, it tries this:

gcc -g -lpthread -lrt -Wall -o scheduler scheduler.o worker.o list.o smp5_tests.o testrunner.o

But I am pretty sure it should be this:

gcc -g -Wall -o scheduler scheduler.o worker.o list.o smp5_tests.o testrunner.o -lpthread -lrt

Here is the Makefile:

CC = gcc
CCOPTS = -c -g -Wall
LINKOPTS = -g -lpthread -lrt -Wall

EXEC=scheduler
OBJECTS=scheduler.o worker.o list.o smp5_tests.o testrunner.o

all: $(EXEC)

$(EXEC):$(OBJECTS) 
        $(CC) $(LINKOPTS) -o $@ $^

1 Answers1

0

I think the issue is that the libraries are at the front of the compile line instead at the end, but I am pretty unfamiliar with a Makefile like this.

Well, this is specific to GNU ld linker, not to make itself: ld resolves dependencies in a single pass from left to right, except between -Wl,--start-group and -Wl,--end-group (which intended specially for handling circular dependencies). This means that the libraries must be put after the modules (or another libraries) which use them.

How can I ensure the links come at the end instead of at the beginning?

Consider how it's done with (a simplified version of) the default rule:

%: %.o
    $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@

Here LDFLAGS are "normal" linker flags which can safely precede the objects list; and LDLIBS is the list of system libraries used by the program.

Matt
  • 11,089
  • 1
  • 10
  • 19