2017-02-13 86 views
0

我有一个非常引渡的问题。makefile,编译源文件列表卡在第一个文件

我正在使用makefile和Eclipse来编译AVR微控制器的程序。基本的想法是创建一个由makefile处理的项目,并且如果我只有一个源文件,那么一切运行良好,但是如果我添加多个源文件,只需将第一个文件编译为对象的时间( .o文件)我想创建。

我认为是与下面的例子更好: 这是我的makefile的snipet:

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) [email protected] 
    @"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 '[email protected] is "[email protected]"'    # print name of object file 
    $(CC) $(CFLAGS) $< -o [email protected] 
    @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" 
[email protected] 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" 
[email protected] 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不会在源列表上运行,它会在第一个源文件上进行搜索。

任何想法或建议吗?

在此先感谢,

namontoy。

+1

从[生成文件文档](https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html):“$ <的**第一**先决条件的名称。“ – kaylum

+0

关于这一行:'SRCS:= $(shell find。-type f -name'* .c')'你在makefile的顶部附近有一个声明,它定义了要使用哪个shell?建议使用如下代码:'SRCS:= $(通配符* .c)' – user3629249

+0

这一行:'$(OBJECTS):$(SRCS)directories'会更好地写成:'%.o:%。c'只有得到第一个文件,因为这一行反复编译:'$(CC)$(CFLAGS)$ <-o $ @',如果使用建议的行,这将会很好 – user3629249

回答

0

这里:

$(OBJECTS) : $(SRCS) directories 
    ... 
    $(CC) $(CFLAGS) $< -o [email protected] 

你让对象取决于所有来源。自动变量$<采用第一个先决条件,它将成为列表中的第一个源文件,无论您尝试构建什么。

尝试pattern rule

%.o : %.c directories 
    @echo compiling $< to produce [email protected] 
    $(CC) $(CFLAGS) $< -o [email protected] 
+0

嗨!非常感谢所有的答复和评论。我做了@beta和@ user3629249两个改变,一切正常,但有一个副作用。 '$(OBJECTS):$(SRCS)directories'我试图在特定的文件夹上生成目标文件,但是现在使用'%.o:%。c目录“,构建过程将对象文件放在主文件夹上,源(.c)文件旁边。我也尝试'$(OBJ_PATH)/%。o:%.c'和'$(OBJ_PATH)/%。o:$(PROJ_DIR)/%。c',但不起作用 – namontoy

+0

@namontoy:不知道为什么'$(OBJ_PATH)/%。o:%.c'没有工作(也不知道它是如何失败的),但这是一个单独的问题 - 这个问题已经在SO上多次提出和回答。 – Beta