C Makefile linker error -

2019-08-28 05:09发布

问题:

I'v written the Makefile below. I'm getting the following error during compilation. I'm not sure what IBS is? Is it missing the L from LIBS or something?

Follow on question:

This is the directory structure.

.
├── Makefile
└── src
    ├── include
    │   └── utils.h
    ├── main.c
    └── utils.c

However, when I run make, this is the output. It doesn't seem to be picking up and building the file utils.c?

cc -I./src -I./src/include -I/usr/local/include/upm -MMD -MP -c src/main.c -o build/./src/main.c.o.o

TARGET_EXEC ?= app.out

BUILD_DIR ?= ./build
SRC_DIRS ?= ./src


SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c -or -name *.s)
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
DEPS := $(OBJS:.o=.d)

INC_DIRS := $(shell find $(SRC_DIRS) -type d)
INC_FLAGS := $(addprefix -I,$(INC_DIRS)) -I/usr/local/include/upm

CPPFLAGS ?= $(INC_FLAGS) -MMD -MP 

$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS)
    $(CC) $(OBJS) -o $@ $(LDFLAGS)

LD_FLAGS = -L/usr/local/lib/upm -L/usr/lib/rabbitmq 

LIBS = -lrabbitmq -lupmc-rn2483 -lupmc-rn2903 -lupmc-utilities

# c source
$(BUILD_DIR)/%.c.o: %.c
    $(MKDIR_P) $(dir $@)
    $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@.o
    $(CC) $(LD_FLAGS)    $@.o -o $@ $LIBS



.PHONY: clean

clean:
    $(RM) -r $(BUILD_DIR)

-include $(DEPS)

MKDIR_P ?= mkdir -p

回答1:

     $(CC) $(LD_FLAGS)    $@.o -o $@ $LIBS
#   wrong                            ^^^^^

is wrong. Makefile variables need parenthesis: $(LIBS) and your $LIBS is understood as $(L)IBS and you don't have any L variable (so $LIBS expands to IBS since $L expands to nothing)

BTW, you could have used make --trace or remake with -x to find that bug

Regarding the edited question, I believe that you might be wrong in having such a complex source tree for such a small program (generally speaking, small programs of less than a few dozen thousands lines are simpler to deal in a flat source tree, that is a single directory containing both *.c and *.h source files). However, you might consider

 INC_DIRS = $(wildcard */include)