2017-08-11 80 views
0

我有一個名爲sources.txt的文本文件中的源文件名列表。在Makefile中,我想讀每一個文件名作爲name從sources.txt幷包括以下行,帶循環的Makefile

name.o: name.c name.h Makefile 
    $(CC) $(CFLAGS) -c name.c 
在Makefile中

。我嘗試創建Makefile。

CFLAGS = -Wall -std=c99 
CC = gcc 

sourcesfile="source/sources.txt" 
# Read every source file name. 
# Produce the corresponding .c, .h and .o extension versions. 
# Then add a file in the makefile as: 
# name.o: name.c name.h Makefile 
# $(CC) $(CFLAGS) -c name.c 
# If the line has main, handle that case separately -- there is no mention of main.h 

while IFS= read -r line; do 
    cfile=$(echo source/${line}.c) 
    hfile=$(echo source/${line}.h) 
    ofile=$(echo source/${line}.o) 
    ofiles=ofiles+$(ofile) 
    if [[ $line == main ]] ; then 
     $(ofile): $(cfile) Makefile 
      $(CC) $(CFLAGS) -c $(cfile) 
    else 
     $(ofile): $(cfile) $(hfile) Makefile 
      $(CC) $(CFLAGS) -c $(cfile) 
    fi 
done < "$sourcesfile" 

genetics:$(ofiles) 
    $(CC) $(CFLAGS) -o genetics $(ofiles) 

clean: 
    $(RM) $(ofiles) 

source/sources.txt是包含C-源文件,但沒有.c擴展名的文本文件。例如,

main 
file1 
file2 
file3 
file4 
file5 

當運行使用make上述生成文件時,它產生下面的錯誤。

Makefile:19: *** commands commence before first target. Stop.

任何人都可以請幫我構建這樣一個Makefile?如果你有一個工作的例子,這將非常感激。

+0

你是混合的shell腳本生成文件?一個makefile有一個非常特定的語法。看起來你想在'sources.txt'中添加依賴關係,順便說一下,還有其他解決方案可以讓它更容易。 – Zeta

+0

你正在使用哪個'make'?這在傳統的UNIX make中是不可能的,但在GNU make中是可能的。 –

+0

你確定你想爲每個文件名分開一個單獨的規則嗎?靜態模式規則不夠好嗎? – Beta

回答