gpt4 book ai didi

c - makefile,编译卡在第一个文件上的源列表

转载 作者:行者123 更新时间:2023-11-30 16:50:21 25 4
gpt4 key购买 nike

我有一个非常奇怪的问题。

我正在使用 makefile 和 Eclipse 来编译 AVR 微 Controller 的程序。基本思想是创建一个由 makefile 处理的项目,如果我只有一 (1) 个源文件,则一切正常,但如果我添加多个源文件,则仅第一个文件被编译与对象一样多的次数( .o 文件)我想创建。

我认为下面的例子更好:这是我的 makefile 的片段:

PROJ_DIR = /home/namontoy/workspace-AVR/exampleBitCloudMakefile
SDK_ROOT = /opt/cross/avr
LIST_PATH = $(CONFIG_NAME)/List
EXE_PATH = $(CONFIG_NAME)/Exe
OBJ_PATH = $(CONFIG_NAME)/Obj

# Create list of sources and list objects
SRCS := $(shell find . -type f -name '*.c')
OBJECTS := $(SRCS:.c=.o)

# Second for to create list of sources and objects
#SOURCES=$(wildcard $(SRCS)/*.c)
#OBJECTS_AUX=$(patsubst %.c, %.o, $(SOURCES))
#OBJECTS = $(subst $(PROJ_DIR),$(OBJ_PATH), $(OBJECTS_AUX))

directories:
@echo
@echo $(MSG_MKDIR) $@
@"mkdir" -p $(LIST_PATH)
@"mkdir" -p $(EXE_PATH)
@"mkdir" -p $(OBJ_PATH)

# Compile: create object files from C source files.
$(OBJECTS) : $(SRCS) directories
@echo
@echo $(MSG_BUILDING) $< # print message of begining build
@echo srcs: $(SRCS) # print list of sources
@echo objects: $(OBJECTS) # print list of objects
@echo '$$< is "$<"' # print file to be compiled
@echo '$$@ is "$@"' # print name of object file
$(CC) $(CFLAGS) $< -o $@
@echo $(MSG_FINISH_BUILDING) $< # print message of finished build

在终端上打印:

Building file: dummyFile.c
srcs: ./dummyFile.c ./LearningInterrupt_Main.c
objects: ./dummyFile.o ./LearningInterrupt_Main.o
$< is "dummyFile.c"
$@ is "dummyFile.o"
avr-gcc -g -c -O1 -std=gnu99 -Wall -mmcu=atmega328p -fshort-enums -funsigned-char -funsigned-bitfields -fpack-struct -Wstrict-prototypes -Wa,-adhlns=dummyFile.lst -I/opt/cross/avr/avr/include -I/opt/cross/avr/lib/gcc/avr/4.8.3/include -I/opt/cross/avr/lib/gcc/avr/4.8.3/include-fixed -I/home/namontoy/workspace-AVR/exampleBitCloudMakefile/headers -I/home/namontoy/workspace-AVR/exampleBitCloudMakefile/ dummyFile.c -o dummyFile.o
Finished building: dummyFile.c

Building file: dummyFile.c
srcs: ./dummyFile.c ./LearningInterrupt_Main.c
objects: ./dummyFile.o ./LearningInterrupt_Main.o
$< is "dummyFile.c"
$@ is "LearningInterrupt_Main.o"
avr-gcc -g -c -O1 -std=gnu99 -Wall -mmcu=atmega328p -fshort-enums -funsigned-char -funsigned-bitfields -fpack-struct -Wstrict-prototypes -Wa,-adhlns=dummyFile.lst -I/opt/cross/avr/avr/include -I/opt/cross/avr/lib/gcc/avr/4.8.3/include -I/opt/cross/avr/lib/gcc/avr/4.8.3/include-fixed -I/home/namontoy/workspace-AVR/exampleBitCloudMakefile/headers -I/home/namontoy/workspace-AVR/exampleBitCloudMakefile/ dummyFile.c -o LearningInterrupt_Main.o
Finished building: dummyFile.c

因此,正如您所看到的,make 读取了所有源文件和对象,但是当要创建第二个对象文件时,make 没有运行源列表,它停留在第一个源文件上。

有什么想法或建议吗?

提前致谢,

纳蒙托伊。

最佳答案

这里:

$(OBJECTS) : $(SRCS) directories
...
$(CC) $(CFLAGS) $< -o $@

您使每个对象都依赖于所有源。以及自动变量$<接受第一个先决条件,无论您要构建什么,它都将是列表中的第一个源文件。

尝试 pattern rule :

%.o : %.c directories
@echo compiling $< to produce $@
$(CC) $(CFLAGS) $< -o $@

关于c - makefile,编译卡在第一个文件上的源列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42214431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com