Makefile 基本写法:
格式:
- -------------------------------------------------
- target: dependencies
- [tab] system command
- -------------------------------------------------
范例1:
- # I am a comment, and I want to say that the variable CC will be
- # the compiler to use.
- CC=g++
- # Hey!, I am comment number 2. I want to say that CFLAGS will be the
- # options I'll pass to the compiler.
- CFLAGS=-c -Wall
- all: hello
- hello: main.o factorial.o hello.o
- g++ main.o factorial.o hello.o -o hello
- main.o: main.cpp
- g++ -c main.cpp
- factorial.o: factorial.cpp
- g++ -c factorial.cpp
- hello.o: hello.cpp
- g++ -c hello.cpp
- clean:
- rm -rf *o hello
范例2:
- 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 $@
- Hello.c