Makefile链接问题 - 未找到

时间:2015-02-10 05:31:21

标签: c makefile

这是我们当前的Makefile;我们试图运行的测试文件包括“phase1.h”。

TARGET = libphase1.a
CFLAGS = -Wall -g
TESTDIR = ./tests
TESTS = forkInvalidParmsTest
LIBS = -lphase1 -lusloss

$(TARGET): phase1.c
    ar -r $@ phase1.c

$(TESTS): $(TARGET) $(TESTDIR)/$@
    gcc $(CFLAGS) -c -I./usloss/build/include $(TARGET) $(TESTDIR)/$@.c
    gcc $(LDFLAGS) -o forkInvalidParmsTest forkInvalidParmsTest.o $(LIBS) ./usloss/lib/linux/libusloss2.8.a

这是我们在尝试运行make forkInvalidParmsTest(成功生成libphase1.a之后)收到的错误。

gcc -Wall -g -c -I./usloss/build/include libphase1.a ./tests/forkInvalidParmsTest.c
./tests/forkInvalidParmsTest.c:2:20: fatal error: phase1.h: No such file or directory
 #include "phase1.h"

有什么想法吗?

更新

自添加-I后错误已更改。对cflags:

make forkInvalidParmsTest                                                                                                                                                                    2 ↵  ✖ ✹master 
gcc -Wall -g -I. -c -I./usloss/build/include libphase1.a ./tests/forkInvalidParmsTest.c
gcc: warning: libphase1.a: linker input file unused because linking not done
gcc  -o forkInvalidParmsTest forkInvalidParmsTest.o -lphase1 -lusloss ./usloss/lib/linux/libusloss2.8.a
/usr/bin/ld: cannot find -lphase1
/usr/bin/ld: cannot find -lusloss
collect2: error: ld returned 1 exit status
make: *** [forkInvalidParmsTest] Error 1


.
├── children_list.c
├── children_list.h
├── children_list_test.c
├── forkInvalidParmsTest.c
├── forkInvalidParmsTest.o
├── kernel.c
├── libphase1.a
├── Makefile
├── phase1.c
├── phase1.h
├── queue.c
├── queue.h
├── tests
│   ├── children_list_test.c
│   └── forkInvalidParmsTest.c
├── usloss
│   ├── 
│       
└── usyscall.h

1 个答案:

答案 0 :(得分:0)

我与你分享了我的示例makefile(我无法在comment部分输入所有内容,如果没有回答你的问题,请告诉我们):

这是我的文件夹视图:

./
├── debug
│   ├── debug.c
│   ├── debug.h
│   └── uart_print.c
├── driver
│   ├── driver.c
│   └── driver.h
├── include
│   └── common.h
├── Makefile
├── root
│   └── main.c
├── tc
│   ├── boot.c
│   ├── boot.h
│   ├── connect.c
│   ├── connect.h
│   ├── ffs.c
│   ├── ffs.h
│   ├── gpio.c
│   ├── gpio.h
│   ├── i2c.c
│   ├── i2c.h
│   ├── network.c
│   ├── network.h
│   ├── power.c
│   ├── power.h
│   ├── product.c
│   ├── product.h
│   ├── spi.c
│   └── spi.h
└── utility
    ├── utility.c
    └── utility.h

Makefile1,没有生成lib

CC        := gcc
LD        := gcc 
COLOR_ON    := color
COLOR_OFF   :=

PROGRAM = gSmokeTest

MAKE_DIR    = $(PWD)
MODULES     := debug driver utility tc root
SRC_DIR     := $(addprefix $(MAKE_DIR)/,$(MODULES))
BUILD_DIR   := $(MAKE_DIR)/output

SRC         := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.c))
OBJ         := $(patsubst %.c,%.o,$(SRC))

INCLUDES    := $(addprefix -I,$(SRC_DIR))
INCLUDES    += -I$(MAKE_DIR)/include

CFLAGS :=
CFLAGS += -Wall -O -ggdb -Wstrict-prototypes -Wno-pointer-sign #-Werror# -finstrument-functions -fdump-rtl-expand
#CFLAGS += -D_DEBUG_ -D_REENTRANT
TC_DEF :=
#TC_DEF += -D_POWER_SOS_
#TC_DEF += -D_PRODUCT_
#TC_DEF += -D_CONNECT_
TC_DEF += -D_SPI_
#TC_DEF += -D_I2C_
#TC_DEF += -D_POWER_
#TC_DEF += -D_BOOT_
#TC_DEF += -D_NETWORK_

vpath %.c $(SRC_DIR)

define make-goal
$1/%.o: %.c
    @$(COLOR_ON)$(CC) $(TC_DEF) $(CFLAGS) $(INCLUDES) -c $$< -o $$@
    @echo "Compile    $$*.c"
endef

.PHONY: all checkdirs clean help flowchart
all: checkdirs $(BUILD_DIR)/$(PROGRAM)

$(BUILD_DIR)/$(PROGRAM): $(OBJ)
    @$(LD) $^ -o $@
    @echo "Generate   $(PROGRAM)"


checkdirs: $(BUILD_DIR)

$(BUILD_DIR):
    mkdir -p $@

clean: 
    rm -f $(OBJ) $(BUILD_DIR)/$(PROGRAM)
    rm -rf $(BUILD_DIR)

help:
    @echo "SRC DIR:    $(SRC_DIR)"
    @echo "Build DIR:  $(BUILD_DIR)"
    @echo "Source:     $(SRC)"
    @echo "Obj:        $(OBJ)"
    @echo "Includes:   $(INCLUDES)"

flowchart:
    @cflow2dot pdf ${SRC}

$(foreach sdir,$(SRC_DIR),$(eval $(call make-goal,$(sdir))))

然后,我设置了另一个文件夹结构(简单,删除一些文件),这次我生成了lib:

./
├── debug
│   ├── debug.h
│   └──debug.mk
├── driver
│   ├── driver.h
│   ├── driver.mk
│   └── driver.o
├── include
│   └── common.h
├── libs
│   ├── libdebug.a
│   ├── libdrv.a
│   ├── libtc.a
│   └── libutility.a
├── make
│   ├── connect.d
│   ├── debug.d
│   ├── driver.d
│   ├── makefile
│   ├── network.d
│   ├── uart_print.d
│   └── utility.d
├── Makefile
├── prog
│   └── DEMO.map
├── root
│   ├── main.c
│   └── root.mk
├── rules.mk
├── tc
│   ├── connect.h
│   ├── network.h
│   └── tc.mk
├── tools.mk
└── utility
    ├── utility.h
    └──utility.mk

Makefile2.1,top makefile:

CC        := gcc
LD        := gcc 

MAKE_DIR    = $(PWD)
MODULES     := root tc utility driver debug
SRC_DIR     := $(addprefix ${MAKE_DIR}/,$(MODULES))
#BUILD_DIR  := $(addprefix ${MAKE_DIR}/,$(MODULES))

SRC         := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.c))
#OBJ            := $(patsubst ${SRC_DIR}/%.c,${BUILD_DIR}/%.o,$(SRC))
#OBJ            := $(foreach sdir,$(SRC_DIR),$(patsubst $(sdir)/%.c,$(BUILD_DIR)/%.o,$(filter $(sdir)/%,$(SRC))))
OBJ         := $(patsubst %.c,%.o,$(SRC))

INCLUDES    := $(addprefix -I,$(SRC_DIR))

vpath %.c $(SRC_DIR)

#SRC_F = $(foreach sdir,$(SRC_DIR),$(filter $(SRC_DIR)/%,$(SRC)))
#OBJ_F = $(patsubst $(SRC_F)/%.c, $(BUILD_DIR)/%.o, $(SRC_F))

default:
    @echo "SRC DIR:    ${SRC_DIR}"
#   @echo "Build DIR:  ${BUILD_DIR}"
    @echo "Source:     ${SRC}"
    @echo "Obj:        ${OBJ}"
    @echo "Includes:   ${INCLUDES}"
#   @echo "SRC_F:      ${SRC_F}"
#   @echo "OBJ_F:      ${OBJ_F}"


define make-goal
$1/%.o: %.c
    $(CC) $(INCLUDES) -c $$< -o $$@
endef

all: test

test: $(OBJ)
    $(LD) $^ -o $@


#$(foreach sdir,$(SRC_DIR),$(eval $(call make-goal,$(sdir))))
#.PHONY: all checkdirs clean
#
#all: checkdirs build/test
#
#build/test: $(OBJ)
#   $(LD) $^ -o $@
#
#
#checkdirs: $(BUILD_DIR)
#
#$(BUILD_DIR):
#   @mkdir -p $@
#
clean:
    rm -f $(OBJ) test
#
$(foreach sdir,$(SRC_DIR),$(eval $(call make-goal,$(sdir))))
#

在每个模块中,我都有单独的mk文件,生成了自己的lib:

#you just need change this MODULE name
MODULE = debug
LIB  = $(LIB_DIR)/lib$(MODULE).a
SRCS = $(wildcard *.c)
OBJS = $(patsubst %.c, %.o, $(SRCS))
DEPS = $(patsubst %.c, %.d, $(SRCS))

$(LIB): $(OBJS)
    @mkdir -p ../libs
    @$(AR) cr $@ $^
    @echo "    Archive   $(notdir $@)"

#$(OBJS): $(SRCS)
%.o: %.c
    @$(CC) $(CFLAGS) -o $@ -c $<
    @echo "    CC        $@"

# Automatic dependency magic:
#%.d: %.c
#   $(CC) -MM $(CFLAGS) $@ > $<

#-include (SRCS:%.c=%.d)

.PHONY: clean
clean:
    @$(RM) -f $(LIB) $(OBJS) $(DEPS)
    @$(RM) -f *.expand
    @echo "    Remove Objects:     $(OBJS)"
    @echo "    Remove Libraries:   $(notdir $(LIB))"
    @echo "    Remove Dependency:  $(DEPS)"

.PHONY: lint 
lint:
    $(LINT) $(INC_SRCH_PATH) $(SRCS)