Makefile:编译和链接后不删除对象

时间:2014-07-14 01:41:39

标签: c++ makefile

以下makefile工作,但在编译和链接后清除目标文件除外。我尝试了make clean,它完全符合我的要求:删除所有文件夹中的可执行文件和目标文件。我还包括makemake clean的输出。有什么想法吗?

生成文件:

CC=g++
CFLAGS=-c -std=c++11 -O2 -O3
SOURCES=main.cpp\
        BoundaryConditions/BoundaryConditions.cpp\
        Cell/Cell.cpp\
        Face/Face.cpp\
        Formulation/Explicit/Explicit.cpp\
        Formulation/Implicit/Implicit.cpp\
        Grid/Grid.cpp\
        Grid/ReadGrid.cpp\
        Grid/SetGrid.cpp\
        Init/Init.cpp\
        InterFlux/Interflux.cpp\
        InterFlux/Roe/Roe.cpp\
        Matrix5/Operators.cpp\
        Output/Output.cpp\
        Solver/GaussSeidel.cpp\
        Vector/Vector.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=codeBaku

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS) 
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
    $(CC) $(CFLAGS) $< -o $@

.PHONY: clean
clean:
    rm -rf $(OBJECTS) $(EXECUTABLE)

make的输出:

g++ -c -std=c++11 -O2 -O3 main.cpp -o main.o
g++ -c -std=c++11 -O2 -O3 BoundaryConditions/BoundaryConditions.cpp -o BoundaryConditions/BoundaryConditions.o
g++ -c -std=c++11 -O2 -O3 Cell/Cell.cpp -o Cell/Cell.o
g++ -c -std=c++11 -O2 -O3 Face/Face.cpp -o Face/Face.o
g++ -c -std=c++11 -O2 -O3 Formulation/Explicit/Explicit.cpp -o Formulation/Explicit/Explicit.o
g++ -c -std=c++11 -O2 -O3 Formulation/Implicit/Implicit.cpp -o Formulation/Implicit/Implicit.o
g++ -c -std=c++11 -O2 -O3 Grid/Grid.cpp -o Grid/Grid.o
g++ -c -std=c++11 -O2 -O3 Grid/ReadGrid.cpp -o Grid/ReadGrid.o
g++ -c -std=c++11 -O2 -O3 Grid/SetGrid.cpp -o Grid/SetGrid.o
g++ -c -std=c++11 -O2 -O3 Init/Init.cpp -o Init/Init.o
g++ -c -std=c++11 -O2 -O3 InterFlux/Interflux.cpp -o InterFlux/Interflux.o
g++ -c -std=c++11 -O2 -O3 InterFlux/Roe/Roe.cpp -o InterFlux/Roe/Roe.o
g++ -c -std=c++11 -O2 -O3 Matrix5/Operators.cpp -o Matrix5/Operators.o
g++ -c -std=c++11 -O2 -O3 Output/Output.cpp -o Output/Output.o
g++ -c -std=c++11 -O2 -O3 Solver/GaussSeidel.cpp -o Solver/GaussSeidel.o
g++ -c -std=c++11 -O2 -O3 Vector/Vector.cpp -o Vector/Vector.o
g++  main.o BoundaryConditions/BoundaryConditions.o Cell/Cell.o Face/Face.o Formulation/Explicit/Explicit.o Formulation/Implicit/Implicit.o Grid/Grid.o Grid/ReadGrid.o Grid/SetGrid.o Init/Init.o InterFlux/Interflux.o InterFlux/Roe/Roe.o Matrix5/Operators.o Output/Output.o Solver/GaussSeidel.o Vector/Vector.o -o codeBaku

make clean的输出:

rm -rf main.o BoundaryConditions/BoundaryConditions.o Cell/Cell.o Face/Face.o Formulation/Explicit/Explicit.o Formulation/Implicit/Implicit.o Grid/Grid.o Grid/ReadGrid.o Grid/SetGrid.o Init/Init.o InterFlux/Interflux.o InterFlux/Roe/Roe.o Matrix5/Operators.o Output/Output.o Solver/GaussSeidel.o Vector/Vector.o codeBaku

1 个答案:

答案 0 :(得分:4)

自动删除目标文件是不常见的,因为这意味着每次都必须重新编译所有内容,即使您只更改了一个源文件。但是,如果你真的想这样做,你可以这样做:

all: $(SOURCES) $(EXECUTABLE)
        rm $(OBJECTS)