简体   繁体   中英

C Makefile linker error -

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.coo

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
     $(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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM