什么是make文件格式

时间:2017-02-06 15:15:31

标签: makefile

我用gedit为我的迷你项目写了一个makefile。 当我运行" make"时,创建所有* .o和可执行文件。 一旦我更改了我的一个文件(没有make clean或make -B) 它显示它再次编译chaneged文件并再次链接所有对象。 但是可执行文件的工作方式没什么改变。 (" make -B"可执行文件运行正常)

CC = g++
CFLAGS = -Wall
OBJS = pawn.o knight.o bishop.o rook.o queen.o king.o board.o                           
movement.o game.o 

all: ex1

ex1: $(OBJS)
    $(CC) $(CFLAGS) $(OBJS) -o ex1


pawn.o: chess/tools/pawn.cpp chess/tools/tool.hpp
    $(CC) $(CFLAGS) -c chess/tools/pawn.cpp

knight.o: chess/tools/knight.cpp chess/tools/tool.hpp
    $(CC) $(CFLAGS) -c chess/tools/knight.cpp

bishop.o: chess/tools/bishop.cpp chess/tools/tool.hpp
    $(CC) $(CFLAGS) -c chess/tools/bishop.cpp

rook.o: chess/tools/rook.cpp chess/tools/tool.hpp
    $(CC) $(CFLAGS) -c chess/tools/rook.cpp

queen.o: chess/tools/queen.cpp chess/tools/tool.hpp
    $(CC) $(CFLAGS) -c chess/tools/queen.cpp

king.o: chess/tools/king.cpp chess/tools/tool.hpp
    $(CC) $(CFLAGS) -c chess/tools/king.cpp

board.o: chess/board.cpp chess/board.hpp
    $(CC) $(CFLAGS) -c chess/board.cpp

movement.o: chess/movement.cpp
    $(CC) $(CFLAGS) -c chess/movement.cpp

game.o: chess/game.cpp
    $(CC) $(CFLAGS) -c chess/game.cpp 

clean:
    rm *.o

如果make文件的设计存在其他问题或建议,我将很高兴听到它。

编辑

1)程序不会在屏幕上打印任何内容。 2)更改是在king.cpp文件(位于另一个文件夹中)打印打印更改到屏幕

  [av@ArchlinuxAvichai chess]$ ls
  chess  ex1  Makefile
  [av@ArchlinuxAvichai chess]$ ls chess
  board.cpp  board.hpp  game.cpp  movement.cpp  tools
  [av@ArchlinuxAvichai chess]$ ls chess/tools
  bishop.cpp  king.cpp  knight.cpp  pawn.cpp  queen.cpp  rook.cpp     tool.hpp

  [av@ArchlinuxAvichai chess]$ make
  g++ -Wall -c chess/tools/pawn.cpp
  g++ -Wall -c chess/tools/knight.cpp
  g++ -Wall -c chess/tools/bishop.cpp
  g++ -Wall -c chess/tools/rook.cpp
  g++ -Wall -c chess/tools/queen.cpp
  g++ -Wall -c chess/tools/king.cpp
  g++ -Wall -c chess/board.cpp
  g++ -Wall -c chess/movement.cpp
  g++ -Wall -c chess/game.cpp 
  g++ -Wall pawn.o knight.o bishop.o rook.o queen.o king.o board.o   movement.o game.o  -o ex1
  [av@ArchlinuxAvichai chess]$ ./ex1
  [av@ArchlinuxAvichai chess]$ *** AT this stage i change the file
  [av@ArchlinuxAvichai chess]$ 
  [av@ArchlinuxAvichai chess]$ make
  g++ -Wall -c chess/tools/king.cpp
  g++ -Wall pawn.o knight.o bishop.o rook.o queen.o king.o board.o  movement.o game.o  -o ex1
  [av@ArchlinuxAvichai chess]$ ./ex1
  [av@ArchlinuxAvichai chess]$ 
  [av@ArchlinuxAvichai chess]$ make -B
  g++ -Wall -c chess/tools/pawn.cpp
  g++ -Wall -c chess/tools/knight.cpp
  g++ -Wall -c chess/tools/bishop.cpp
  g++ -Wall -c chess/tools/rook.cpp
  g++ -Wall -c chess/tools/queen.cpp
  g++ -Wall -c chess/tools/king.cpp
  g++ -Wall -c chess/board.cpp
  g++ -Wall -c chess/movement.cpp
  g++ -Wall -c chess/game.cpp 
  g++ -Wall pawn.o knight.o bishop.o rook.o queen.o king.o board.o  movement.o game.o  -o ex1
  [av@ArchlinuxAvichai chess]$ ./ex1
  Print Change
  [av@ArchlinuxAvichai chess]$

1 个答案:

答案 0 :(得分:0)

尝试以下makefile:

CXX := g++
CXXFLAGS := -Wall
OBJS := pawn.o knight.o bishop.o rook.o queen.o king.o board.o movement.o game.o

COMPILE_CMD = ${CXX} -c -o $@ ${CXXFLAGS} -MD -MP $<
LINK_CMD = ${CXX} -o $@ ${LDFLAGS} $^

all: ex1

ex1: ${OBJS}
    ${LINK_CMD}

%.o : chess/tools/%.cpp
    ${COMPILE_CMD}

%.o : chess/%.cpp
    ${COMPILE_CMD}

-include $(OBJS:%.o=%.d)

clean:
    rm -f *.o *.d

.PHONY: clean all

的变化:

  1. ${CXX}用于C ++代码。
  2. 自动生成标头依赖项。
  3. 使用模式规则以避免重复。
  4. 指定.o输出文件。
  5. 将定位cleanall标记为.PHONY
  6. 使用LDFLAGS进行关联。