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

标签 c eclipse makefile

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

我正在使用 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/

相关文章:

c++ - 如何解决eclipse中cplex的目录问题?

c++ - 制作 : c: command not found

c - make : 'file' is up to date, 后面什么都不做

c++ - 如何判断给定路径是目录还是文件? (C/C++)

c - 测量执行时间

c - 无法使用 fread 从文件读取数据

eclipse - 在 Eclipse 中管理多个分支,或为 Eclipse 获取类似 VS 的设置

java - 有没有办法为 JavaFX 类提供一个新样式的类,然后正常访问该类中的所有其他对象?

makefile - make[1] make[2] make[3] 中的数字是什么意思?

c - 将新节点添加到列表时出现段错误