Makefile 简明教程

Makefile - Example

以下是用 Makefile 编译 hello 程序的示例。该程序包含三个文件 main.cpp、factorial.cpp 和 hello.cpp。

This is an example of the Makefile for compiling the hello program. This program consists of three files main.cpp, factorial.cpp and hello.cpp.

# Define required macros here
SHELL = /bin/sh

OBJS =  main.o factorial.o hello.o
CFLAG = -Wall -g
CC = gcc
INCLUDE =
LIBS = -lm

hello:${OBJ}
   ${CC} ${CFLAGS} ${INCLUDES} -o $@ ${OBJS} ${LIBS}

clean:
   -rm -f *.o core *.core

.cpp.o:
   ${CC} ${CFLAGS} ${INCLUDES} -c $<

现在,您可以使用 hello 构建程序 make 。如果您将发出命令 make clean ,则它将删除当前目录中所有可用的目标文件和核心文件。

Now you can build your program hello using the make. If you will issue a command make clean then it removes all the object files and core files available in the current directory.