2016-06-10 10 views
0

ich mit diesem Code meine C-Quelldateien kompilieren:Schleife durch Liste der Dateien in Makefile

CC=clang -std=c11 
CFLAGS=-Wall -g 
ASSEMBLY=-S -masm=intel 
OPTIMIZE=-Ofast 

FOLDER_SRC=./src/ 
FOLDER_BIN=./bin/ 
FOLDER_ASSEMBLY=./ass/ 

clean: 
    rm -f \ 
    $(FOLDER_BIN)1.1-hello $(FOLDER_ASSEMBLY)1.1-hello \ 
    $(FOLDER_BIN)1.2-fahrenheit $(FOLDER_ASSEMBLY)1.2-fahrenheit \ 
    $(FOLDER_BIN)1.2-fahrenheit-floating $(FOLDER_ASSEMBLY)1.2-fahrenheit-floating 

all: 
    $(CC) $(CFLAGS) $(OPTIMIZE) $(FOLDER_SRC)1.1-hello.c -o $(FOLDER_BIN)1.1-hello 
    $(CC) $(CFLAGS) $(OPTIMIZE) $(FOLDER_SRC)1.2-fahrenheit.c -o $(FOLDER_BIN)1.2-fahrenheit 
    $(CC) $(CFLAGS) $(OPTIMIZE) $(FOLDER_SRC)1.2-fahrenheit-floating.c -o $(FOLDER_BIN)1.2-fahrenheit-floating 

assembly: 
    $(CC) $(ASSEMBLY) $(OPTIMIZE) $(FOLDER_SRC)1.1-hello.c -o $(FOLDER_ASSEMBLY)1.1-hello 
    $(CC) $(ASSEMBLY) $(OPTIMIZE) $(FOLDER_SRC)1.2-fahrenheit.c -o $(FOLDER_ASSEMBLY)1.2-fahrenheit 
    $(CC) $(ASSEMBLY) $(OPTIMIZE) $(FOLDER_SRC)1.2-fahrenheit-floating.c -o $(FOLDER_ASSEMBLY)1.2-fahrenheit-floating 

Ich versuche, eine Art Schleife zu implementieren weniger zu schreiben. Ich habe diesen Code, aber es nur Fehler generieren:

FILENAME_SRC := $(wildcard $(FOLDER_SRC)*.c) 
FILENAME_BUILD := $(patsubst $(FOLDER_SRC)%.c,%,$(FILENAME_SRC)) 

echo : $(FILENAME_SRC) 
    @echo $^ 
    @echo $(FILENAME_BUILD) 

build : $(FILENAME_SRC) 
    $(CC) $(CFLAGS) $(OPTIMIZE) $^ -o $(FILENAME_BUILD) 

make echo Druck dies auf der Konsole:

$ make echo 
src/1.2-fahrenheit.c src/1.1-hello.c src/1.2-fahrenheit-floating.c 
1.2-fahrenheit 1.1-hello 1.2-fahrenheit-floating 

make build Befehl zu erzeugen diesen Fehler:

$ make build 
clang -std=c11 -Wall -g -Ofast src/1.2-fahrenheit.c src/1.1-hello.c src/1.2-fahrenheit-floating.c -o 1.2-fahrenheit 1.1-hello 1.2-fahrenheit-floating 
clang.exe: error: no such file or directory: '1.1-hello' 
clang.exe: error: no such file or directory: '1.2-fahrenheit-floating' 
make: *** [Makefile:21: build] Error 1 

ich die erwartete Ausgabe wollen aussehen wie wenn ich mache make all:

$ make all 
clang -std=c11 -Wall -g -Ofast ./src/1.1-hello.c -o ./bin/1.1-hello 
clang -std=c11 -Wall -g -Ofast ./src/1.2-fahrenheit.c -o ./bin/1.2-fahrenheit 
clang -std=c11 -Wall -g -Ofast ./src/1.2-fahrenheit-floating.c -o ./bin/1.2-fahrenheit-floating 

Endlösung

CC := clang -std=c11 
GCC := gcc -std=c11 
CFLAGS := -Wall -g 
ASSEMBLY := -Wall -S -masm=intel 
OPTIMIZE := -O3 -march=native 

SRC := $(wildcard src/*.c) 
BIN := $(patsubst src/%.c,bin/%,$(SRC)) 
ASS := $(patsubst src/%.c,ass/%,$(SRC)) 

clean: 
    rm -f bin/* ass/* 

build: $(BIN) 
$(BIN): bin/%: src/%.c 
    $(CC) $(CFLAGS) $(OPTIMIZE) $^ -o [email protected] 

assembly: $(ASS) 
$(ASS): ass/%: src/%.c 
    $(CC) $(ASSEMBLY) $(OPTIMIZE) $^ -o [email protected] 

Wirklich leid für die clean ass/* aber es kann nicht assembly weil die ähnlichen Ordnernamen im Projekt aufgerufen werden.

Antwort

2

Ihre all Regel braucht so etwas wie

CC := clang 
CFLAGS := -std=c11 -Wall -g -Ofast 

targets := bin/1.1-hello bin/1.2-fahrenheit bin/1.2-fahrenheit-floating 

.PHONY: all 
all: $(targets) 
$(targets): bin/%: src/%.c 
    $(LINK.c) $^ $(LDLIBS) -o [email protected] 
+0

Dieser genaue Code creatint die folgende Fehlermeldung zu sehen: 'make: *** Keine Regel Ziel 'src/1.2-farenheit.c' zu machen, benötigt durch "bin/1,2-Fahrenheit". Stop.' – Lanti

+0

Tut mir leid, es funktioniert, Sie haben nur 'Fahrenheit' vertippt, deshalb nicht auf meinem PC kompiliert. – Lanti

+1

@Lanti Hoppla, behoben. – user657267

Verwandte Themen