2017-02-13 85 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