开发者

Questions about makefile

Here is开发者_运维百科 an easy makefile.

I have 2 questions.

  1. all: $(SOURCES) $(EXECUTABLE) Why put the SOURCE in the dependency.
  2. ".cpp.o:" Why not write ".o: .cpp"

    CC=g++
    
    CFLAGS=-c -Wall
    
    LDFLAGS=
    
    SOURCES=main.cpp hello.cpp factorial.cpp
    
    OBJECTS=$(SOURCES:.cpp=.o)
    
    EXECUTABLE=hello
    
    
    all: $(SOURCES) $(EXECUTABLE)
    
    $(EXECUTABLE): $(OBJECTS) 
    
        $(CC) $(LDFLAGS) $(OBJECTS) -o $@
    
    .cpp.o:
    
        $(CC) $(CFLAGS) $< -o $@
    


The dependency of all on $(SOURCES) is not necessary or even useful. The dependency information should be such that the executable depends on the object files, and the object files depend on the source files.

The notation:

.cpp.o:

was the way the original (7th Edition UNIX™) version of make handled compilation rules. GNU Make (and Sun Make) used the % notation to allow:

%.o: %.cpp

Basically, it was a design decision that made sense at the time and maybe less sense in retrospect. It was not the most egregious problem (that would be tabs at the start of command lines).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜