Makefile:如何将自动生成的文件放在单独的目录中?

时间:2018-06-05 08:05:47

标签: makefile compiler-errors directory

您可以在此处查看我的Makefile:

....
PHY_SRCS = ../LwMesh/phy/at86rf212/src/phy.c
HAL_SRCS = ../LwMesh/hal/atmega1281/src/hal.c ../LwMesh/hal/atmega1281/src/halPhy.c ../LwMesh/hal/atmega1281/src/halTimer.c
SYS_SRCS = ../LwMesh/sys/src/sys.c  ../LwMesh/sys/src/sysEncrypt.c  ../LwMesh/sys/src/sysTimer.c
....



# define the C object files
PHY_OBJS = $(PHY_SRCS:.c=.o)
HAL_OBJS = $(HAL_SRCS:.c=.o)
....



# define the executable file
PHY = phy_cc
HAL = hal_cc
SYS = sys_cc
.....

OBJDIR := obj
$(OBJDIR):
    mkdir $(OBJDIR)


all:  $(PHY)    $(HAL)  $(SYS)  $(DRV)  $(NWK)  $(SRV)  $(VELA)
    @echo  Alles wurde Kompiliert

$(PHY): $(PHY_OBJS)
    $(CC) $(CFLAGS) $(INCLUDES) -o  $(PHY) $(PHY_OBJS) $(LFLAGS)
$(HAL): $(HAL_OBJS)
    $(CC) $(CFLAGS) $(INCLUDES) -o  $(HAL) $(HAL_OBJS) $(LFLAGS)

    ....


.c.o:
    $(CC) $(CFLAGS) $(INCLUDES) -c $<  -o $@

...

我已经尝试过将自动生成的文件(例如目标文件)放在单独的目录(OBJDIR)中,但我没有找到任何解决方案。 我提到this tutorial,但它不适合我的Makefile。 你能帮我吗?

2 个答案:

答案 0 :(得分:2)

让我们采取一些小步骤。

您已经有了构建目标文件的规则:

.c.o:
    $(CC) $(CFLAGS) $(INCLUDES) -c $<  -o $@

可以写成:

%.o: %.c
    $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

这将使用../LwMesh/phy/at86rf212/src/phy.c来构建../LwMesh/phy/at86rf212/src/phy.c。但想象一下phy.c在工作目录中。然后,Make可以使用该规则从phy.o构建phy.c。在这种情况下,我们可以对规则进行一些小改动:

$(OBJDIR)/%.o: %.c
    $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

和Make将构建obj/phy.o

我们如何告诉在哪里找到phy.c?有两种好方法。一个是使用vpath

vpath %.c ../LwMesh/phy/at86rf212/src

在我们确认这是有效的之后,我们可以为其他对象集添加其他目录(例如PHYSYS):

vpath %.c ../LwMesh/phy/at86rf212/src ../LwMesh/hal/atmega1281/src ../LwMesh/sys/src ...

现在我们要做的就是正确构建对象列表:

PHY_OBJS = phy.o
HAL_OBJS = hal.o halPhy.o halTimer.o
SYS_OBJS = sys.o sysEncrypt.o sysTimer.o
...

一旦这么多工作正常,就可以进一步改进。

答案 1 :(得分:1)

您可以在此处看到更正后的Makefile:

# define compiler type
CC = avr-gcc
# define any compile-time flags
CFLAGS = -Wall -g   -funsigned-char -funsigned-bitfields -DPHY_AT86RF212 -DHAL_ATMEGA1281 -DPLATFORM_ANY900_STICK  -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -mrelax -g2 -Wall -mmcu=atmega1281 -c -std=gnu99 -MD -MP -MF "$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -MT"$(@:%.o=%.o)"

# define any directories containing header files other than /usr/include
INCLUDES +=  \
-I"../LwMesh/hal/atmega1281/inc" \
-I"../LwMesh/hal/drivers/atmega1281" \
-I"../LwMesh/phy/at86rf212/inc" \
-I"../LwMesh/nwk/inc" \
-I"../LwMesh/sys/inc" \
-I"../LwMesh/service/inc" \
-I"../common" \
-I".." \
-I.
# define library paths in addition to /usr/lib
LFLAGS = -L/home/newhall/lib  -L../lib

# define any libraries to link into executable:
LIBS = -lmylib -lm

# define the C source files
PHY_SRCS = ../LwMesh/phy/at86rf212/src/phy.c
HAL_SRCS = ../LwMesh/hal/atmega1281/src/hal.c ../LwMesh/hal/atmega1281/src/halPhy.c ../LwMesh/hal/atmega1281/src/halTimer.c
SYS_SRCS = ../LwMesh/sys/src/sys.c  ../LwMesh/sys/src/sysEncrypt.c  ../LwMesh/sys/src/sysTimer.c
DRV_SRCS = ../LwMesh/hal/drivers/atmega1281/halUart.c ../LwMesh/hal/drivers/atmega1281/halTwi.c
NWK_SRCS +=  \
../LwMesh/nwk/src/nwk.c \
../LwMesh/nwk/src/nwkDataReq.c \
../LwMesh/nwk/src/nwkSecurity.c \
../LwMesh/nwk/src/nwkFrame.c \
../LwMesh/nwk/src/nwkGroup.c \
../LwMesh/nwk/src/nwkRoute.c \
../LwMesh/nwk/src/nwkRouteDiscovery.c \
../LwMesh/nwk/src/nwkRx.c \
../LwMesh/nwk/src/nwkTx.c
SRV_SRCS = ../LwMesh/service/src/otaClient.c    ../LwMesh/service/src/otaServer.c
VELA_SRCS +=  \
base-commands.c \
bus-commands.c \
bus-interface.c \
host-interface.c \
measurement.c \
VelaMain.c \
otaInterface.c \
persistence.c \
shell.c

PHY_OBJS = $(addprefix $(OBJDIR)/PHY/,phy.o)
HAL_OBJS = $(addprefix $(OBJDIR)/HAL/,hal.o halPhy.o halTimer.o)
SYS_OBJS = $(addprefix $(OBJDIR)/SYS/,sys.o sysEncrypt.o    sysTimer.o)
DRV_OBJS = $(addprefix $(OBJDIR)/DRV/,halUart.o halTwi.o)
NWK_OBJS = $(addprefix $(OBJDIR)/NWK/,nwk.o nwkDataReq.o nwkSecurity.o nwkFrame.o nwkGroup.o nwkRoute.o nwkRouteDiscovery.o nwkRx.o nwkTx.o)
SRV_OBJS = $(addprefix $(OBJDIR)/SRV/,otaClient.o   otaServer.o)
VELA_OBJS = $(addprefix $(OBJDIR)/VELA/,base-commands.o bus-commands.o bus-interface.o host-interface.o measurement.o VelaMain.o otaInterface.o persistence.o shell.o)

# define the executable file
PHY = phy_cc
HAL = hal_cc
SYS = sys_cc
DRV = drv_cc
NWK = nwk_cc
SRV = srv_cc
VELA = vela_cc


OBJDIR := objdir
OUTPUT_FILE_PATH +=Vela2.elf
OUTPUT_FILE_PATH_AS_ARGS +=Vela2.elf
OUTPUT_FILE_DEP:=
ALL_OBJS := $(PHY_OBJS) $(HAL_OBJS) $(SYS_OBJS) $(DRV_OBJS) $(NWK_OBJS) $(SRV_OBJS) $(VELA_OBJS)
ALL_SRCS := $(PHY_SRCS) $(HAL_SRCS) $(SYS_SRCS) $(DRV_SRCS) $(NWK_SRCS) $(SRV_SRCS) $(VELA_SRCS)

$(OUTPUT_FILE_PATH): $(ALL_OBJS) $(OUTPUT_FILE_DEP)
    @echo Invoking: AVR/GNU Linker
    $(CC) -o$(OUTPUT_FILE_PATH_AS_ARGS) $(ALL_OBJS) -Wl,-Map="Vela2.map" -Wl,-u,vfprintf -Wl,--start-group  -Wl,--end-group -Wl,--gc-sections -mrelax -mmcu=atmega1281
    @echo Finished building target: $@
    avr-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature -R .user_signatures  "Vela2.elf" "Vela2.hex"
    avr-objcopy -j .eeprom  --set-section-flags=.eeprom=alloc,load --change-section-lma .eeprom=0  --no-change-warnings -O ihex "Vela2.elf" "Vela2.eep" || exit 0
    avr-objdump -h -S "Vela2.elf" > "Vela2.lss"
    avr-objcopy -O srec -R .eeprom -R .fuse -R .lock -R .signature -R .user_signatures "Vela2.elf" "Vela2.srec"
    avr-size "Vela2.elf"


$(OBJDIR)/PHY/%.o : ../LwMesh/phy/at86rf212/src/%.c
    $(CC) $(CFLAGS) $(INCLUDES) $(OUTPUT_OPTION)  $<

$(OBJDIR)/HAL/%.o : ../LwMesh/hal/atmega1281/src/%.c
    $(CC) $(CFLAGS) $(INCLUDES) $(OUTPUT_OPTION)  $<

$(OBJDIR)/SYS/%.o : ../LwMesh/sys/src/%.c
    $(CC) $(CFLAGS) $(INCLUDES) $(OUTPUT_OPTION)  $<

$(OBJDIR)/DRV/%.o : ../LwMesh/hal/drivers/atmega1281/%.c
    $(CC) $(CFLAGS) $(INCLUDES) $(OUTPUT_OPTION)  $<

$(OBJDIR)/NWK/%.o : ../LwMesh/nwk/src/%.c
    $(CC) $(CFLAGS) $(INCLUDES) $(OUTPUT_OPTION)  $<

$(OBJDIR)/SRV/%.o : ../LwMesh/service/src/%.c
    $(CC) $(CFLAGS) $(INCLUDES) $(OUTPUT_OPTION)  $<

$(OBJDIR)/VELA/%.o : %.c
    $(CC) $(CFLAGS) $(INCLUDES) $(OUTPUT_OPTION)  $<

all: $(ALL_OBJS)    $(OUTPUT_FILE_PATH)

$(ALL_OBJS): | $(OBJDIR)

$(OBJDIR):
    mkdir -p objdir/PHY
    mkdir -p objdir/HAL
    mkdir -p objdir/SYS
    mkdir -p objdir/DRV
    mkdir -p objdir/NWK
    mkdir -p objdir/SRV
    mkdir -p objdir/VELA

$(PHY): $(PHY_OBJS)
    $(CC) $(CFLAGS) $(INCLUDES) -o  $(PHY) $(PHY_OBJS) $(LFLAGS)

$(HAL): $(HAL_OBJS)
    $(CC) $(CFLAGS) $(INCLUDES) -o  $(HAL) $(HAL_OBJS) $(LFLAGS)

$(SYS): $(SYS_OBJS)
    $(CC) $(CFLAGS) $(INCLUDES) -o  $(SYS) $(SYS_OBJS) $(LFLAGS)

$(DRV): $(DRV_OBJS)
    $(CC) $(CFLAGS) $(INCLUDES) -o  $(DRV) $(DRV_OBJS) $(LFLAGS)

$(NWK): $(NWK_OBJS)
    $(CC) $(CFLAGS) $(INCLUDES) -o  $(NWK) $(NWK_OBJS) $(LFLAGS)

$(SRV): $(SRV_OBJS)
    $(CC) $(CFLAGS) $(INCLUDES) -o  $(SRV) $(SRV_OBJS) $(LFLAGS)

$(VELA): $(VELA_OBJS)
    $(CC) $(CFLAGS) $(INCLUDES) -o  $(VELA) $(VELA_OBJS) $(LFLAGS)

clean:
        -$(RM) $(OBJS_AS_ARGS) $(EXECUTABLES)
        -$(RM) $(C_DEPS_AS_ARGS)
        rm -rf "Vela2.elf" "Vela2.a" "Vela2.hex" "Vela2.lss" "Vela2.eep" "Vela2.map" "Vela2.srec" "Vela2.usersignatures"


depend: $(PHY_SRCS) $(HAL_SRCS) $(SYS_SRCS) $(DRV_SRCS) $(NWK_SRCS) $(SRV_SRCS) $(VELA_SRCS)
    makedepend $(INCLUDES) $^